package com.iformall.controller;


import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxMerchant;
import com.iformall.domain.vo.WxBillAll;
import com.iformall.enums.EnumFilterSettle;
import com.iformall.enums.EnumMerchantStatus;
import com.iformall.service.WxBillAllService;
import com.iformall.service.WxBillSettleService;
import com.iformall.service.WxMerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
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 java.util.List;
import java.util.Map;

/**
 * @author gongbiao
 */
@RestController
@Api(description = "账单")
@RequestMapping("/api/bill")
public class WxBillController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxBillAllService wxBillAllService;
    @Autowired
    WxMerchantService wxMerchantService;
    @Autowired
    WxBillSettleService wxBillSettleService;

    @ApiOperation("所有欠缴和待缴的数据")
    @PostMapping("listBillOweAndWaitPay")
    public ResultData listBillOweAndWaitPay(@RequestBody WxBillAll wxBillAll) {

        if(StringUtils.isEmpty(wxBillAll.getTenantId())){
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"tenantId不能为空");
        }
        if(wxBillAll.getMerchantId()==null){
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"merchantId不能为空");
        }
        WxMerchant wxMerchant = wxMerchantService.getById(wxBillAll.getMerchantId());
        if(wxMerchant==null){
            return new ResultData(ErrorCode.MERCHANT_INFO_NOT_FOUND);
        }
        if(wxMerchant.getStatus().equals(EnumMerchantStatus.NOT_VALID.getCode())){
            return new ResultData(ErrorCode.MERCHANT_INFO_NOT_VALID);
        }
        List<Map<String,Object>> result=wxBillAllService.listBillOweAndWaitPay(wxBillAll);
        return new ResultData(result);
    }

    @ApiOperation("账单")
    @PostMapping("listBill")
    public ResultData listBill(@RequestBody WxBillAll wxBillAll) {

        if(StringUtils.isEmpty(wxBillAll.getTenantId())){
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"tenantId不能为空");
        }
        if(wxBillAll.getMerchantId()==null){
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"merchantId不能为空");
        }
        WxMerchant wxMerchant = wxMerchantService.getById(wxBillAll.getMerchantId());
        if(wxMerchant==null){
            return new ResultData(ErrorCode.MERCHANT_INFO_NOT_FOUND);
        }
        if(wxMerchant.getStatus().equals(EnumMerchantStatus.NOT_VALID.getCode())){
            return new ResultData(ErrorCode.MERCHANT_INFO_NOT_VALID);
        }
        if(!StringUtils.isEmpty(wxBillAll.getStarttime()) && StringUtils.isEmpty(wxBillAll.getEndtime())){
            logger.info("开始时间和结束时间要同时存在或不存在");
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"starttime和endtime要同时存在或不存在");
        }
        if(StringUtils.isEmpty(wxBillAll.getStarttime()) && !StringUtils.isEmpty(wxBillAll.getEndtime())){
            logger.info("开始时间和结束时间要同时存在或不存在");
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(),"starttime和endtime要同时存在或不存在");
        }
        return wxBillAllService.listBill(wxBillAll);
    }


    @GetMapping("/findSettleById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @ApiOperation("账单管理-结算单-id查找")
    public ResultData findById(Long id) {
        return new ResultData(Result.SUCCESS, "查询成功", wxBillSettleService.getById(id, EnumFilterSettle.NO.getCode()));
    }

}
