package com.iformall.controller.rent;

import com.alibaba.fastjson.JSONArray;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.WxPayAccountBill;
import com.iformall.domain.vo.WxBillAll;
import com.iformall.domain.vo.WxBillExcelTemplate;
import com.iformall.service.WxBillAllService;
import com.iformall.service.WxPayAccountBillService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
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;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author gongbiao
 */
@RestController
@RequestMapping("wxBillAll")
public class WxBillAllController extends BaseController {
    @Autowired
    private WxBillAllService wxBillAllService;

    private Logger logger = LoggerFactory.getLogger(WxBillAllController.class);

    @Autowired
    WxPayAccountBillService wxPayAccountBillService;

    @GetMapping("oweBillCount")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractId", value = "合同id", dataType = "long", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "类型1租赁合同2物业合同", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "账单总览-总额")
    public ResultData oweBillCount(Long contractId, Integer type) {
        Long count = wxBillAllService.hasOweBill(contractId, type);
        return new ResultData(count);
    }

    @GetMapping("list")
    @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 WxBillAll wxBillAll, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxBillAllController::list");
        WxPayAccountBill wxPayAccountBill = wxPayAccountBillService.getByTenantId(wxBillAll.getTenantId());
        if (null == wxBillAll) {
            wxBillAll = new WxBillAll();
        }
        wxBillAll.setTenantId(getTenantId());
        if(StringUtils.isNotBlank(wxBillAll.getStatusListStr())){
            wxBillAll.setStatusList(new ArrayList<>());
            JSONArray jsonArray = JSONArray.parseArray(wxBillAll.getStatusListStr());
            for (int i = 0; i <jsonArray.size() ; i++) {
                Integer o = (Integer) jsonArray.get(i);
                wxBillAll.getStatusList().add(o);
            }
        }
        if(StringUtils.isNotBlank(wxBillAll.getTypeListStr())){
            wxBillAll.setTypeList(new ArrayList<>());
            JSONArray jsonArray = JSONArray.parseArray(wxBillAll.getTypeListStr());
            for (int i = 0; i <jsonArray.size() ; i++) {
                Integer o = (Integer) jsonArray.get(i);
                wxBillAll.getTypeList().add(o);
            }
        }
        Map<String, Object> result = wxBillAllService.listAsPage(wxBillAll, pageNum, pageSize);
        return new ResultData(result);
    }

    @GetMapping("allDepositList")
    @SystemControllerLog(description = "账单总览-所有押金列表")
    public ResultData allDepositList(@ModelAttribute WxBillAll wxBillAll) {
        logger.debug("[" + getIpAddr() + "] WxBillAllController::allDepositList");
        if (null == wxBillAll) {
            wxBillAll = new WxBillAll();
        }
        wxBillAll.setTenantId(getTenantId());
        return new ResultData(wxBillAllService.allDepositList(wxBillAll));
    }

    @GetMapping("exportBill")
    @SystemControllerLog(description = "账单总览-导出")
    public void exportBill(@ModelAttribute WxBillAll wxBillAll, HttpServletRequest request, HttpServletResponse response) {
        wxBillAll.setTenantId(getTenantId());
        wxBillAllService.exportBill(request, response, wxBillAll);
    }

    @GetMapping("getBillInfo")
    @SystemControllerLog(description = "账单总览-获取账单信息")
    public ResultData getBillInfo(@ModelAttribute WxBillAll wxBillAll) {
        wxBillAll.setTenantId(getTenantId());
        Object billInfo = wxBillAllService.getBillInfo(wxBillAll);
        return new ResultData(billInfo);
    }


    /**
     * 欠缴商户列表
     * @param wxBillAll
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("oweMerchantlist")
    @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 oweMerchantlist(@ModelAttribute WxBillAll wxBillAll, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxBillAllController::oweMerchantlist");
        if (null == wxBillAll) {
            wxBillAll = new WxBillAll();
        }
        //默认查当月第一天到今天
        if(StringUtils.isNotBlank(wxBillAll.getEndtime())){
            wxBillAll.setEndtime(wxBillAll.getEndtime()+" 23:59:59");
        }
        wxBillAll.setTenantId(getTenantId());
        Map<String, Object> result = wxBillAllService.getOweBillAsPage(wxBillAll, pageNum, pageSize);
        return new ResultData(result);
    }

    /**
     * 应收、承付商户列表
     * @param wxBillAll
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("receiveAndPayMerchantlist")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "账单管理-应收、承付商户列表")
    @ApiOperation("应收、承付商户列表")
    public ResultData receiveAndPayMerchantlist(@ModelAttribute WxBillAll wxBillAll, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxBillAllController::receiveAndPayMerchantlist");
        if (null == wxBillAll) {
            wxBillAll = new WxBillAll();
        }
        wxBillAll.setTenantId(getTenantId());
        Map<String, Object> result = wxBillAllService.getReceiveAndPayBillAsPage(wxBillAll, pageNum, pageSize);
        return new ResultData(result);
    }

    /**
     * 导出应收、承付商户列表
     * @param wxBillAll
     * @return
     */
    @GetMapping("exportReceiveAndPayMerchantlist")
    @SystemControllerLog(description = "账单管理-导出应收、承付商户列表")
    @ApiOperation("导出应收、承付商户列表")
    public void exportReceiveAndPayMerchantlist(@ModelAttribute WxBillAll wxBillAll,HttpServletRequest request, HttpServletResponse response) {
        logger.debug("[" + getIpAddr() + "] WxBillAllController::exportReceiveAndPayMerchantlist");
        if (null == wxBillAll) {
            wxBillAll = new WxBillAll();
        }
        wxBillAll.setTenantId(getTenantId());
        wxBillAllService.exportReceiveAndPayBillAsPage(wxBillAll, request,response);
    }


    @ApiOperation("导出欠缴商户列表")
    @GetMapping("/exportOweMerchantlist")
    @SystemControllerLog(description = "费用收缴数据-欠缴商户列表-导出")
    public void exportOweMerchantlist(@ModelAttribute WxBillAll wxBillAll, HttpServletRequest request, HttpServletResponse response) {
        logger.debug("[" + getIpAddr() + "] WxBillAllController::exportOweMerchantlist");
        if (null == wxBillAll){
            wxBillAll = new WxBillAll();
        }
        //默认查当月第一天到今天
        if(StringUtils.isNotBlank(wxBillAll.getEndtime())){
            wxBillAll.setEndtime(wxBillAll.getEndtime()+" 23:59:59");
        }
        wxBillAll.setTenantId(getTenantId());
        wxBillAllService.exportOweMerchant(wxBillAll, request, response);
    }

    @ApiOperation("导出待缴商户列表")
    @GetMapping("/exportWaitMerchantlist")
    @SystemControllerLog(description = "费用收缴数据-待缴商户列表-导出")
    public void exportWaitMerchantlist(@ModelAttribute WxBillAll wxBillAll, HttpServletRequest request, HttpServletResponse response) {
        logger.debug("[" + getIpAddr() + "] WxBillAllController::exportWaitMerchantlist");
        if (null == wxBillAll){
            wxBillAll = new WxBillAll();
        }
        //默认查当月第一天到今天
        if(StringUtils.isNotBlank(wxBillAll.getEndtime())){
            wxBillAll.setEndtime(wxBillAll.getEndtime()+" 23:59:59");
        }
        wxBillAll.setTenantId(getTenantId());
        wxBillAllService.exportWaitMerchant(wxBillAll, request, response);
    }


    @GetMapping("exportOweBill")
    @SystemControllerLog(description = "费用收缴数据-生成催缴单")
    public void exportOweBill(@ModelAttribute WxBillAll wxBillAll, HttpServletRequest request, HttpServletResponse response) {
        logger.debug("[" + getIpAddr() + "] WxBillAllController::exportOweBill");
        if (null == wxBillAll) {
            wxBillAll = new WxBillAll();
        }
        //默认查当月第一天到今天
        if (StringUtils.isNotBlank(wxBillAll.getEndtime())) {
            wxBillAll.setEndtime(wxBillAll.getEndtime() + " 23:59:59");
        }
        wxBillAll.setTenantId(getTenantId());
        wxBillAllService.exportOweBill(wxBillAll, request, response);
    }

    @PostMapping("updateReceivePay")
    @SystemControllerLog(description = "账单-更新")
    public ResultData updateReceivePay(@RequestBody WxBillAll wxBillAll) {
        logger.debug("[" + getIpAddr() + "] WxBillRentController::add");
        wxBillAll.setTenantId(getTenantId());
        return wxBillAllService.updateReceivePay(wxBillAll, getUser());
    }


    @GetMapping("exportTemplate")
    @SystemControllerLog(description = "账单-导出模板")
    public void exportTemplate(@ModelAttribute WxBillExcelTemplate wxBillExcelTemplate, HttpServletRequest request, HttpServletResponse response) {
        logger.debug("[" + getIpAddr() + "] WxBillAllController::exportTemplate");
        wxBillAllService.exportTemplate(wxBillExcelTemplate, request, response);
    }

}
