package com.iformall.controller.rent;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxBillSettle;
import com.iformall.domain.po.WxBillSettleRecord;
import com.iformall.enums.EnumFilterSettle;
import com.iformall.service.WxBillAllService;
import com.iformall.service.WxBillSettleRecordService;
import com.iformall.service.WxBillSettleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author luozukai
 */
@RestController
@RequestMapping("wxBillSettle")
public class WxBillSettleController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxBillSettleService wxBillSettleService;
    @Autowired
    private WxBillSettleRecordService wxBillSettleRecordService;
    @Autowired
    private WxBillAllService wxBillAllService;

    @GetMapping("list")
    @ApiOperation("账单管理-结算对冲列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "账单管理-结算对冲列表")
    public ResultData list(@ModelAttribute WxBillSettle wxBillSettle, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxBillSettleController::list");
        if (null == wxBillSettle) {
            wxBillSettle = new WxBillSettle();
        }
        wxBillSettle.setTenantId(getTenantId());
        wxBillSettle.setSortColumns(BaseEntity.SortField.Createtime_DESC,BaseEntity.SortField.Id_DESC);
        final PageInfo<WxBillSettle> page = wxBillSettleService.listAsPage(wxBillSettle, pageNum, pageSize);
        return new ResultData(page);
    }

    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @ApiOperation("账单管理-结算单-id查找")
    @SystemControllerLog(description = "账单管理-结算单-id查找")
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxBillSettleController::findById");
        return new ResultData(Result.SUCCESS, "查询成功", wxBillSettleService.getById(id, EnumFilterSettle.YES.getCode()));
    }

    @PostMapping("addOrUpdate")
    @ApiOperation("账单管理-结算单-新增或编辑")
    @SystemControllerLog(description = "账单管理-结算单-新增或编辑")
    public ResultData addOrUpdate(@RequestBody  WxBillSettle wxBillSettle) {
        logger.debug("[" + getIpAddr() + "] WxBillSettleController::addOrUpdate");
        wxBillSettle.setTenantId(getTenantId());
        return wxBillSettleService.saveOrUpdate(wxBillSettle,getUser());
    }

    @PostMapping("settle")
    @ApiOperation("账单管理-结算单-结算")
    @SystemControllerLog(description = "账单管理-结算单-结算")
    public ResultData settle(@RequestBody WxBillSettleRecord wxBillSettleRecord) {
        logger.debug("[" + getIpAddr() + "] WxBillSettleController::settle");
        wxBillSettleRecord.setTenantId(getTenantId());
        return wxBillSettleRecordService.saveOrUpdate(wxBillSettleRecord,getUser(),null);
    }

    @GetMapping("exportSettle")
    @SystemControllerLog(description = "账单管理-结算单-导出")
    public void exportBill(@ModelAttribute WxBillSettle wxBillSettle, HttpServletRequest request, HttpServletResponse response) {
        wxBillSettle.setTenantId(getTenantId());
        wxBillSettleService.exportBill(request, response, wxBillSettle);
    }

    @GetMapping("downloadSettle")
    @SystemControllerLog(description = "账单管理-结算单-下载")
    public void downloadSettle(@ModelAttribute WxBillSettle wxBillSettle, HttpServletRequest request, HttpServletResponse response) {
        wxBillSettle.setTenantId(getTenantId());
        wxBillAllService.exportSettleBill(wxBillSettle,request,response);
    }
}
