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.*;
import com.iformall.domain.vo.WxCardSpendVo;
import com.iformall.domain.vo.WxCardVo;
import com.iformall.exception.MallinkException;
import com.iformall.service.WxCardInfoService;
import com.iformall.service.WxCardSpendService;
import com.iformall.service.WxMerchantService;
import com.iformall.service.WxOrderService;
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.List;
import java.util.Map;

/**
 * @author Stormeye Wu wuguoqiang@iformall.com
 */
@RestController
@RequestMapping("cardPay")
@Api(description = "储值卡支付-C端扫B端商户码")
public class WxCardPayController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxMerchantService wxMerchantService;

    @Autowired
    WxOrderService wxOrderService;

    @Autowired
    WxCardInfoService wxCardInfoService;

    @Autowired
    WxCardSpendService wxCardSpendService;

    @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 cardOrderList(@ModelAttribute WxCardVo wxCardVo, Integer pageNum, Integer pageSize) {
        String ipStr = getIpAddr();
        logger.info("cardPay/cardOrderList: " + ipStr);
        if (wxCardVo == null) {wxCardVo = new WxCardVo();}
        else {
            if(StringUtils.isBlank(wxCardVo.getOuPhone())) {
                wxCardVo.setOuPhone(null);
            }
        }
        wxCardVo.setTenantId(getTenantId());
        if(StringUtils.isNotBlank(wxCardVo.getStatusStr())) {
            String [] statusAttr = wxCardVo.getStatusStr().split(",");
            List<Integer> tmpList = new ArrayList<Integer>();
            for(String status: statusAttr) {
                tmpList.add(Integer.valueOf(status));
            }
            if(!tmpList.isEmpty()) {
                wxCardVo.setStatusS(tmpList);
            }
        }
        final PageInfo<WxCardVo> page = wxCardInfoService.listAsPage(wxCardVo, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("C端扫B端储值卡交易流水(消费记录 + 补贴)列表接口")
    @GetMapping("cardSpendlist")
    @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 cardSpendlist(@ModelAttribute WxCardSpendVo wxCardSpendVo, Integer pageNum, Integer pageSize) {
        String ipStr = getIpAddr();
        logger.info("cardPay/cardSpendlist: " + ipStr);
        if (wxCardSpendVo == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件为空");
        }
        wxCardSpendVo.setTenantId(getTenantId());
        if(StringUtils.isNotBlank(wxCardSpendVo.getPayStatusStr())) {
            String [] statusAttr = wxCardSpendVo.getPayStatusStr().split(",");
            List<Integer> tmpList = new ArrayList<Integer>();
            for(String status: statusAttr) {
                tmpList.add(Integer.valueOf(status));
            }
            if(!tmpList.isEmpty()) {
                wxCardSpendVo.setPayStatusS(tmpList);
            }
        }
        final PageInfo<WxCardSpendVo> page = wxCardSpendService.listAsPage(wxCardSpendVo, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("C端扫B端储值卡交易流水SUM")
    @GetMapping("sum")
    @SystemControllerLog(description = "储值卡支付-储值卡交易流水SUM")
    public ResultData sumCardSpendList(@ModelAttribute WxCardSpendVo wxCardSpend) {
        String ipStr = getIpAddr();
        logger.info("cardPay/sumCardSpendList: " + ipStr + " ：" + wxCardSpend.toString());
        if (wxCardSpend == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件为空");
        }
        wxCardSpend.setTenantId(getTenantId());
        final List<Map<String, Object>> mapList = wxCardSpendService.sumCardSpendForMerchant(wxCardSpend);
        return new ResultData(mapList);
    }

    @ApiOperation("交易流水导出")
    @GetMapping("/exportData")
    @SystemControllerLog(description = "储值卡支付-储值卡交易流水导出")
    public void exportData(@ModelAttribute WxCardSpendVo wxCardSpendVo, HttpServletRequest request, HttpServletResponse response) {
        logger.info("[" + getIpAddr() + "] cardPay/exportData");
        if (wxCardSpendVo == null) {
            throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件为空");
        }
        wxCardSpendVo.setTenantId(getTenantId());
        if(StringUtils.isNotBlank(wxCardSpendVo.getPayStatusStr())) {
            String [] statusAttr = wxCardSpendVo.getPayStatusStr().split(",");
            List<Integer> tmpList = new ArrayList<Integer>();
            for(String status: statusAttr) {
                tmpList.add(Integer.valueOf(status));
            }
            if(!tmpList.isEmpty()) {
                wxCardSpendVo.setPayStatusS(tmpList);
            }
        }
        wxCardSpendService.exportData(wxCardSpendVo, request, response);
    }

    @ApiOperation("更新卡消费支付状态")
    @PostMapping("/updatePayStatus")
    @SystemControllerLog(description = "储值卡支付-更新卡消费支付状态")
    public ResultData update(@RequestBody WxCardSpend wxCardSpend) {
        String ipStr = getIpAddr();
        logger.info("cardPay/updatePayStatus: " + ipStr + " ：" + wxCardSpend.toString());
        if (wxCardSpend == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件为空");
        }
        wxCardSpend.setTenantId(getTenantId());
        wxCardSpendService.update(wxCardSpend);
        return new ResultData();
    }

    @ApiOperation("卡完结")
    @PostMapping("/finishCard")
    @SystemControllerLog(description = "储值卡支付-卡完结")
    public ResultData finishCard(@RequestBody WxCardInfo wxCardInfo) {
        String ipStr = getIpAddr();
        logger.info("cardPay/finishCard: " + ipStr + " ：" + wxCardInfo.toString());
        if (wxCardInfo == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件为空");
        }
        if (wxCardInfo.getId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "过滤条件ID为空");
        }
        String tenantId = getTenantId();
        wxCardInfo.setTenantId(tenantId);
        int remainShareAmount = wxCardSpendService.finishCard(tenantId, wxCardInfo.getId());
        return new ResultData(remainShareAmount);
    }

    @ApiOperation("卡订单-导出数据")
    @GetMapping("/exportCardOrder")
    @SystemControllerLog(description = "卡订单-导出数据")
    public void exportCardOrder(@ModelAttribute WxCardVo wxCardVo, HttpServletRequest request, HttpServletResponse response) {
        logger.info("[" + getIpAddr() + "] cardPay/exportCardOrder");
        if (wxCardVo == null) {
            wxCardVo = new WxCardVo();
        } else {
            if (StringUtils.isBlank(wxCardVo.getOuPhone())) {
                wxCardVo.setOuPhone(null);
            }
        }
        wxCardVo.setTenantId(getTenantId());
        if (StringUtils.isNotBlank(wxCardVo.getStatusStr())) {
            String[] statusAttr = wxCardVo.getStatusStr().split(",");
            List<Integer> tmpList = new ArrayList<Integer>();
            for (String status : statusAttr) {
                tmpList.add(Integer.valueOf(status));
            }
            if (!tmpList.isEmpty()) {
                wxCardVo.setStatusS(tmpList);
            }
        }
        wxCardInfoService.exportData(wxCardVo, request, response);
    }

    @ApiOperation("卡消费订单-导出数据")
    @GetMapping("exportCardSpend")
    @SystemControllerLog(description = "卡消费订单-导出数据")
    public void exportCardSpend(@ModelAttribute WxCardSpendVo wxCardSpendVo, HttpServletRequest request, HttpServletResponse response) {
        String ipStr = getIpAddr();
        logger.info("cardPay/exportCardSpend: " + ipStr);
        if (wxCardSpendVo == null) {
            wxCardSpendVo = new WxCardSpendVo();
        }
        wxCardSpendVo.setTenantId(getTenantId());
        if (StringUtils.isNotBlank(wxCardSpendVo.getPayStatusStr())) {
            String[] statusAttr = wxCardSpendVo.getPayStatusStr().split(",");
            List<Integer> tmpList = new ArrayList<Integer>();
            for (String status : statusAttr) {
                tmpList.add(Integer.valueOf(status));
            }
            if (!tmpList.isEmpty()) {
                wxCardSpendVo.setPayStatusS(tmpList);
            }
        }
        wxCardSpendService.exportData(wxCardSpendVo, request, response);

    }

}
