package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxCoupon;
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("listCoupon")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    public ResultData listCoupon(@ModelAttribute WxCoupon wxCoupon, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxCouponController::listCoupon");
        if (wxCoupon == null) wxCoupon = new WxCoupon();
        wxCoupon.setTenantId(getTenantId());
        return wxCouponService.listType(wxCoupon, pageNum, pageSize, 0); //券列表
    }

    @ApiOperation("卡分页列表接口")
    @GetMapping("listCard")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    public ResultData listCard(@ModelAttribute WxCoupon wxCoupon, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxCouponController::listCard");
        if (wxCoupon == null) wxCoupon = new WxCoupon();
        wxCoupon.setTenantId(getTenantId());
        return wxCouponService.listType(wxCoupon, pageNum, pageSize, EnumCouponType.CARD_MULTIMCH.getCode()); //卡列表
    }

    @ApiOperation("砍价分页列表接口")
    @GetMapping("listPress")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    public ResultData listPress(@ModelAttribute WxCoupon wxCoupon, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxCouponController::listPress");
        if (wxCoupon == null) wxCoupon = new WxCoupon();
        wxCoupon.setTenantId(getTenantId());
        return wxCouponService.listType(wxCoupon, pageNum, pageSize, EnumCouponType.COUPON_PRESS.getCode()); //砍价列表
    }

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

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

    @ApiOperation("根据id更新接口")
    @PostMapping("update")
    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)
    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)
    public ResultData findById(@RequestParam Long id) {
        logger.debug("[" + getIpAddr() + "] WxCouponController::findById");;
        return new ResultData(wxCouponService.getVoById(id));
    }

    @ApiOperation("卡类列表接口")
    @GetMapping("findList")
    public ResultData findList(@ModelAttribute WxCoupon wxCoupon) {
        return new ResultData(wxCouponService.list(wxCoupon));
    }

    @ApiOperation("卡营销列表接口")
    @GetMapping("findCardCountList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    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<WxCoupon> pageInfo = wxCouponService.findCountData(wxCoupon,pageNum,pageSize);
        for (WxCoupon 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)})
    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<WxCoupon> pageInfo = wxCouponService.findCouponData(wxCoupon,pageNum,pageSize);
        for (WxCoupon 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)})
    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));
    }




}
