package com.iformall.controller.market;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.vo.WxMerchantSubsidyVo;
import com.iformall.enums.EnumMerchantSubsidyStatus;
import com.iformall.exception.MallinkException;
import com.iformall.service.WxMerchantSubsidyService;
import io.swagger.annotations.Api;
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.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("subsidy")
@Api(description = "商户补贴接口")
public class WxMerchantSubsidyController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxMerchantSubsidyService wxMerchantSubsidyService;

    @ApiOperation("补贴记录汇总")
    @GetMapping("sum")
    @SystemControllerLog(description = "商户-补贴记录-汇总")
    public ResultData count(@ModelAttribute WxMerchantSubsidyVo wxMerchantSubsidy) {
        String ipStr = getIpAddr();
        logger.info("subsidy/sum: " + ipStr + " ：" + wxMerchantSubsidy.toString());
        if (wxMerchantSubsidy == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件为空");
        }
        wxMerchantSubsidy.setTenantId(getTenantId());
        if(StringUtils.isNotBlank(wxMerchantSubsidy.getStatusStr())) {
            String [] statusAttr = wxMerchantSubsidy.getStatusStr().split(",");
            List<Integer> tmpList = new ArrayList<Integer>();
            for(String status: statusAttr) {
                tmpList.add(Integer.valueOf(status));
            }
            if(!tmpList.isEmpty()) {
                wxMerchantSubsidy.setStatusS(tmpList);
            }
        }
        List<Map<String, Object>> retMap = wxMerchantSubsidyService.sumForSubsidy(wxMerchantSubsidy);
        return new ResultData(retMap);
    }

    @ApiOperation("补贴记录")
    @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 listMonth(@ModelAttribute WxMerchantSubsidyVo wxMerchantSubsidy, Integer pageNum, Integer pageSize) {
        String ipStr = getIpAddr();
        logger.info("subsidy/list: " + ipStr + " ：" + wxMerchantSubsidy.toString());
        if (wxMerchantSubsidy == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件为空");
        }
        wxMerchantSubsidy.setTenantId(getTenantId());
        if(StringUtils.isNotBlank(wxMerchantSubsidy.getStatusStr())) {
            String [] statusAttr = wxMerchantSubsidy.getStatusStr().split(",");
            List<Integer> tmpList = new ArrayList<Integer>();
            for(String status: statusAttr) {
                tmpList.add(Integer.valueOf(status));
            }
            if(!tmpList.isEmpty()) {
                wxMerchantSubsidy.setStatusS(tmpList);
            }
        }
        wxMerchantSubsidy.setSortColumns(BaseEntity.SortField.CreateDate_DESC);
        final PageInfo<WxMerchantSubsidyVo> page = wxMerchantSubsidyService.listAsPage(wxMerchantSubsidy, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("交易列表")
    @GetMapping("summary")
    @SystemControllerLog(description = "结算-总计")
    public ResultData summary(@ModelAttribute WxMerchantSubsidyVo wxMerchantSubsidy) {
        logger.debug("[" + getIpAddr() + "] subsidy::summary");
        if (null == wxMerchantSubsidy) {
            wxMerchantSubsidy = new WxMerchantSubsidyVo();
        }
        wxMerchantSubsidy.setTenantId(getTenantId());
        Map<String, Long> data = wxMerchantSubsidyService.summary(wxMerchantSubsidy);
        return new ResultData(data);
    }

    @ApiOperation("更新补贴记录")
    @PostMapping("updateSubsidied")
    @SystemControllerLog(description = "商户-更新补贴记录")
    public ResultData updateSubsidied(@RequestBody WxMerchantSubsidyVo wxMerchantSubsidy) {
        String ipStr = getIpAddr();
        logger.info("subsidy/update: " + ipStr + " ：" + wxMerchantSubsidy.toString());
        if (wxMerchantSubsidy == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件为空");
        }
        wxMerchantSubsidy.setTenantId(getTenantId());
        wxMerchantSubsidy.setStatus(EnumMerchantSubsidyStatus.MANUAL_SUBSIDIED.getCode());
        wxMerchantSubsidy.setUpdateDate(new Date());
        if(StringUtils.isNotBlank(wxMerchantSubsidy.getIdStr())) {
            String [] idArr = wxMerchantSubsidy.getIdStr().split(",");
            List<Long> tmpList = new ArrayList<Long>();
            for(String id: idArr) {
                tmpList.add(Long.valueOf(id));
            }
            if(!tmpList.isEmpty()) {
                wxMerchantSubsidy.setIds(tmpList);
            }
        }
        return wxMerchantSubsidyService.update(wxMerchantSubsidy);
    }

    @ApiOperation("补贴记录导出")
    @GetMapping("/exportData")
    @SystemControllerLog(description = "商户-补贴记录-导出")
    public void exportData(@ModelAttribute WxMerchantSubsidyVo wxMerchantSubsidy, HttpServletRequest request, HttpServletResponse response) {
        logger.info("[" + getIpAddr() + "] subsidy/exportData");
        if (wxMerchantSubsidy == null) {
            throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件不能为空");
        }
        wxMerchantSubsidy.setTenantId(getTenantId());
        if(StringUtils.isNotBlank(wxMerchantSubsidy.getStatusStr())) {
            String [] statusAttr = wxMerchantSubsidy.getStatusStr().split(",");
            List<Integer> tmpList = new ArrayList<Integer>();
            for(String status: statusAttr) {
                tmpList.add(Integer.valueOf(status));
            }
            if(!tmpList.isEmpty()) {
                wxMerchantSubsidy.setStatusS(tmpList);
            }
        }
        wxMerchantSubsidy.setSortColumns(BaseEntity.SortField.CreateDate_DESC);
        wxMerchantSubsidyService.exportData(wxMerchantSubsidy, request, response);
    }

}
