package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxCoupon;
import com.iformall.domain.po.WxCouponSend;
import com.iformall.domain.vo.WxCouponSendVo;
import com.iformall.enums.EnumCouponSendStatus;
import com.iformall.enums.EnumCouponSendType;
import com.iformall.enums.EnumCouponStatus;
import com.iformall.service.WxCouponActionLogService;
import com.iformall.service.WxCouponSendService;
import com.iformall.service.WxCouponService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("wxCouponSend")
public class WxCouponSendController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxCouponSendService wxCouponSendService;
    @Autowired
    private WxCouponService wxCouponService;
    @Autowired
    private WxCouponActionLogService wxCouponActionLogService;

    @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 WxCouponSend wxCouponSend, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxCouponSendController::list");
        if (null == wxCouponSend) wxCouponSend = new WxCouponSend();
        wxCouponSend.setTenantId(getTenantId());
        wxCouponSend.setStatus(EnumCouponSendStatus.VALID.getCode());
        wxCouponSend.setSortColumns(WxCouponSend.Field.CreateDate_DESC);
        final PageInfo<WxCouponSendVo> page = wxCouponSendService.listAsPage(wxCouponSend, pageNum, pageSize);

        for (WxCouponSendVo cs:page.getList())
            cs.setSendCount(wxCouponActionLogService.getCountByChannelId(getTenantId(), cs.getSendType(), cs.getId()));

        return new ResultData(page);
    }

    @ApiOperation("新增接口")
    @PostMapping("add")
    public ResultData add(@RequestBody WxCouponSend wxCouponSend) {
        logger.debug("[" + getIpAddr() + "] WxCouponSendController::add");
        if (null == wxCouponSend) {
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
        }
   
        WxCoupon wxCoupon = wxCouponService.getById(wxCouponSend.getCouponId());
        if (!wxCoupon.getStatus().equals(EnumCouponStatus.COUPON_STATUS_THROW_IN.getCode()) ){
            return new ResultData(ErrorCode.COUPON_IS_TAKE_OFF);
        }

        if (!wxCoupon.getSendType().equals(EnumCouponSendType.PASSIVE.getCode())) {
            return new ResultData(ErrorCode.COUPON_TYPE_IS_NOT_PASSIVE);
        }

        wxCouponSend.setTenantId(wxCoupon.getTenantId());
        wxCouponSend.setTitle(wxCoupon.getTitle());
        wxCouponSend.setCreateDate(new Date());
        wxCouponSend.setUpdateDate(new Date());

        wxCouponSendService.saveOrUpdate(wxCouponSend);
        return new ResultData();
    }

    @ApiOperation("根据id更新接口")
    @PostMapping("update")
    public ResultData update(@RequestBody WxCouponSend wxCouponSend) {
        logger.debug("[" + getIpAddr() + "] WxCouponSendController::update");
        wxCouponSend.setTenantId(getTenantId());
        wxCouponSendService.saveOrUpdate(wxCouponSend);
        return new ResultData();
    }

    @ApiOperation("根据id删除接口")
    @GetMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] WxCouponSendController::delete");
        WxCouponSend wxCouponSend = wxCouponSendService.getById(id);
        if (wxCouponSend == null)
            return new ResultData(ErrorCode.COUPON_SEND_IS_INVALID);
        wxCouponSend.setStatus(EnumCouponSendStatus.INVALID.getCode());
        wxCouponSend.setUpdateDate(new Date());
        wxCouponSendService.saveOrUpdate(wxCouponSend);
        return new ResultData();
    }

    @ApiOperation("根据id查询接口")
    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxCouponSendController::findById");
        return new ResultData(Result.SUCCESS, "查询成功", wxCouponSendService.getById(id));
    }


}
