package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.po.MallUserInfo;
import com.iformall.domain.po.WxMerchantSubsidy;
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
@Api(description = "商户补贴接口")
@RequestMapping("subsidy")
public class WxMerchantSubsidyController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxMerchantSubsidyService wxMerchantSubsidyService;

    @ApiOperation("补贴记录汇总")
    @GetMapping("sum")
    public ResultData count(@ModelAttribute WxMerchantSubsidy wxMerchantSubsidy) {
        String ipStr = getIpAddr();
        logger.info("subsidy/sum: " + ipStr + " ：" + wxMerchantSubsidy.toString());
        if (wxMerchantSubsidy == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件为空");
        }
        MallUserInfo user = getUser();
        wxMerchantSubsidy.setTenantId(user.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);
            }
        }
        Map 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)})
    public ResultData listMonth(@ModelAttribute WxMerchantSubsidy 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(), "过滤条件为空");
        }
        MallUserInfo user = getUser();
        wxMerchantSubsidy.setTenantId(user.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(WxMerchantSubsidy.Field.CreateDate_DESC);
        final PageInfo<WxMerchantSubsidyVo> page = wxMerchantSubsidyService.listAsPage(wxMerchantSubsidy, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("更新补贴记录")
    @PostMapping("updateSubsidied")
    public ResultData updateSubsidied(@RequestBody WxMerchantSubsidy wxMerchantSubsidy) {
        String ipStr = getIpAddr();
        logger.info("subsidy/update: " + ipStr + " ：" + wxMerchantSubsidy.toString());
        if (wxMerchantSubsidy == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件为空");
        }
        MallUserInfo user = getUser();
        wxMerchantSubsidy.setTenantId(user.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")
    public void exportData(@ModelAttribute WxMerchantSubsidy 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(WxMerchantSubsidy.Field.CreateDate_DESC);
        wxMerchantSubsidyService.exportData(wxMerchantSubsidy, request, response);
    }

}
