package com.iformall.controller.market;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.WxCoupon;
import com.iformall.domain.po.WxCouponSend;
import com.iformall.domain.vo.WxCouponSendVo;
import com.iformall.enums.EnumCouponSendSendType;
import com.iformall.enums.EnumCouponSendType;
import com.iformall.enums.EnumCouponStatus;
import com.iformall.exception.MallinkException;
import com.iformall.service.WxCouponActionLogService;
import com.iformall.service.WxCouponSendService;
import com.iformall.service.WxCouponService;
import io.swagger.annotations.Api;
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.Objects;

@RestController
@RequestMapping("wxCouponSend")
@Api(description = "注劵接口")
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),
            @ApiImplicitParam(name = "merchantName", value = "商户名，模糊查询", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "merchantId", value = "商户Id", dataType = "String", paramType = "query"),
    })
    @SystemControllerLog(description = "注券-列表")
    public ResultData list(@ModelAttribute WxCouponSend wxCouponSend, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxCouponSendController::list");
        if (null == wxCouponSend) wxCouponSend = new WxCouponSend();
        wxCouponSend.setTenantId(getTenantId());

//        if (Objects.equals(EnumCouponSendSendType.MERCHANT.getCode(), wxCouponSend.getSendType())) {
//            if (Objects.nonNull(wxCouponSend.getStatus())) {
//                valid = wxCouponSend.getStatus();
//            } else {
//                wxCouponSend.setStatus(null);
//            }
//        }
        PageInfo<WxCouponSendVo> page = wxCouponSendService.listAsPage(wxCouponSend, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("注券-添加配置")
    @GetMapping("config/add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "beforeDays", value = "提前n天发券", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "sendType", value = "注券类型(生日券=8)", dataType = "int", paramType = "query", required = true),
    })
    @SystemControllerLog(description = "注券-添加或更新配置")
    public ResultData addConfig(Integer beforeDays, Integer sendType) {
        if (!Objects.equals(sendType, EnumCouponSendSendType.BIRTHDAY.getCode())) {
            return new ResultData(ErrorCode.COUPON_VALID_NOT_SUPPORTED);
        }
        try {
            wxCouponSendService.saveOrUpdateConfig(beforeDays, sendType, getTenantId());
        } catch (MallinkException e) {
            return new ResultData(e.getErrorCode(), e.getMessage());
        } catch (Exception e) {
            return new ResultData(ErrorCode.SYS_PARAMETER_ERROR, e.getMessage());
        }
        return new ResultData();
    }

    @ApiOperation("注券-获取配置")
    @GetMapping("config/get")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sendType", value = "注券类型(生日券=8)", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "注券-获取配置")
    public ResultData getConfig(Integer sendType) {
        WxCouponSend wxCouponSend = wxCouponSendService.getConfig(sendType, getTenantId());
        return new ResultData(Objects.isNull(wxCouponSend) ? null : wxCouponSend.getConditions());
    }

    @ApiOperation("新增接口")
    @PostMapping("add")
    @SystemControllerLog(description = "注券-新增")
    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 (!EnumCouponStatus.COUPON_STATUS_THROW_IN.getCode().equals(wxCoupon.getStatus())) {
            return new ResultData(ErrorCode.COUPON_IS_TAKE_OFF);
        }

        if (!EnumCouponSendType.PASSIVE.getCode().equals(wxCoupon.getSendType())) {
            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());

        try {
            wxCouponSendService.saveOrUpdate(wxCouponSend);
        } catch (MallinkException e) {
            return new ResultData(e.getErrorCode(), e.getMessage());
        }

        return new ResultData();
    }

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

    @ApiOperation("根据id删除接口")
    @GetMapping("/del")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "sendType", value = "注券类型(会员生日券时必传)", dataType = "int", paramType = "query")
    })
    @SystemControllerLog(description = "注券-删除")
    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);
        }
        try {
            wxCouponSendService.updateInvalid(wxCouponSend);
        } catch (MallinkException e) {
            return new ResultData(e.getErrorCode(), e.getMessage());
        }
        return new ResultData();
    }

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


}
