package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxCoupon;
import com.iformall.domain.vo.WxCouponStatisVo;
import com.iformall.enums.*;
import com.iformall.service.WxCouponService;
import com.iformall.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("wxCoupon")
@Api(description = "优惠券接口")
public class WxCouponController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxCouponService wxCouponService;

    @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 list(@ModelAttribute WxCoupon wxCoupon, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxCouponController::list");
        if (wxCoupon == null) wxCoupon = new WxCoupon();
        wxCoupon.setTenantId(getTenantId());
        return wxCouponService.list(wxCoupon, pageNum, pageSize); //全列表
    }

    @ApiOperation("新增接口")
    @PostMapping("add")
    @SystemControllerLog(description = "券投放-新增")
    public ResultData add(@RequestBody WxCoupon wxCoupon) {
        logger.debug("[" + getIpAddr() + "] WxCouponController::add");
        return wxCouponService.saveOrUpdate(wxCoupon);
    }

    @ApiOperation("根据id更新接口")
    @PostMapping("update")
    @SystemControllerLog(description = "券投放-更新")
    public ResultData update(@RequestBody WxCoupon wxCoupon) {
        logger.debug("[" + getIpAddr() + "] WxCouponController::update");
        if (wxCoupon.getId() == null) {
            return new ResultData(ResultData.ERROR, "缺少id");
        }
        wxCoupon.setTenantId(getTenantId());
        return wxCouponService.saveOrUpdate(wxCoupon);
    }

    @ApiOperation("根据id删除接口")
    @GetMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "券投放-删除")
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] WxCouponController::delete");
        wxCouponService.deleteById(id);
        return new ResultData(Result.SUCCESS, "删除成功", null);
    }

    @ApiOperation("根据id查询接口")
    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "券投放-查询")
    public ResultData findById(@RequestParam Long id) {
        logger.debug("[" + getIpAddr() + "] WxCouponController::findById");;
        return new ResultData(wxCouponService.getVoById(id));
    }


    @ApiOperation("卡营销列表接口")
    @GetMapping("findCardCountList")
    @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 findCardCountList(@ModelAttribute WxCoupon wxCoupon, Integer pageNum, Integer pageSize) {
        //默认时间本月第一天，截止到当天
        Map<String,Object> result = new HashedMap();
        if(wxCoupon.getStartdate() == null){
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH,1);
            wxCoupon.setStartdate(c.getTime());
        }
        if(wxCoupon.getEnddate() == null){
            wxCoupon.setEnddate(DateUtils.stringToDate(DateUtils.getSystemTime("yyyy-MM-dd") + " 23:59:59"));
        }
        PageInfo<WxCouponStatisVo> pageInfo = wxCouponService.findCountData(wxCoupon,pageNum,pageSize);
        for (WxCouponStatisVo coupon:pageInfo.getList()) {
            if(coupon.getSaleAmount() == null){
                coupon.setSaleAmount(0);
            }
            if(coupon.getSumPayment() == null){
                coupon.setSumPayment(0);
            }
            if(coupon.getSumSubsidy() == null){
                coupon.setSumSubsidy(0);
            }
        }
        result.put("list",pageInfo);
        return new ResultData(result);
    }

    @ApiOperation("卷营销列表接口")
    @GetMapping("findCouponCountList")
    @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 findCouponCountList(@ModelAttribute WxCoupon wxCoupon, Integer pageNum, Integer pageSize) {
        //默认时间本月第一天，截止到当天
        Map<String,Object> result = new HashedMap();
        if(wxCoupon.getStartdate() == null){
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH,1);
            wxCoupon.setStartdate(c.getTime());
        }
        if(wxCoupon.getEnddate() == null){
            wxCoupon.setEnddate(DateUtils.stringToDate(DateUtils.getSystemTime("yyyy-MM-dd") + " 23:59:59"));
        }

        PageInfo<WxCouponStatisVo> pageInfo = wxCouponService.findCouponData(wxCoupon,pageNum,pageSize);
        for (WxCouponStatisVo coupon:pageInfo.getList()) {
            if(coupon.getSumSubsidy() == null){
                coupon.setSumSubsidy(0);
            }
            if(coupon.getSumSubsidy() == null){
                coupon.setSumSubsidy(0);
            }
        }
        return new ResultData(pageInfo);
    }

    @ApiOperation("砍价营销列表接口")
    @GetMapping("findPressCountList")
    @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 findPressCountList(@ModelAttribute WxCoupon wxCoupon, Integer pageNum, Integer pageSize) {
        //默认时间本月第一天，截止到当天
        Map<String,Object> result = new HashedMap();
        if(wxCoupon.getStartdate() == null){
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH,1);
            wxCoupon.setStartdate(c.getTime());
        }
        if(wxCoupon.getEnddate() == null){
            wxCoupon.setEnddate(DateUtils.stringToDate(DateUtils.getSystemTime("yyyy-MM-dd") + " 23:59:59"));
        }
        return new ResultData(wxCouponService.findPressData(wxCoupon,pageNum,pageSize));
    }




}
