package com.bweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighCoupon;
import com.hai.entity.HighDiscountCouponRel;
import com.hai.model.ResponseData;
import com.hai.service.HighCouponService;
import com.hai.service.HighDiscountCouponRelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/4/3 23:25
 */
@Controller
@RequestMapping(value = "/discountCoupon")
@Api(value = "优惠券和卡卷的关系接口")
public class HighDiscountCouponRelController {

    private static Logger log = LoggerFactory.getLogger(HighDiscountCouponRelController.class);

    @Resource
    private HighDiscountCouponRelService highDiscountCouponRelService;

    @Resource
    private HighCouponService highCouponService;

    @RequestMapping(value="/insertDiscountCoupon",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加优惠券和卡券关系")
    public ResponseData insertDiscountCoupon(@RequestBody String reqBody) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(reqBody);
            Long discountId = jsonObject.getLong("discountId");
            String couponIdStr = jsonObject.getString("couponIdStr");

            if (discountId == null || StringUtils.isBlank(couponIdStr)) {
                log.error("HighDiscountCouponRelController -> insertDiscountCoupon() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            String[] couponId = couponIdStr.split(",");
            List<HighDiscountCouponRel> list = new ArrayList<>();
            HighDiscountCouponRel rel;
            for (String id : couponId) {
                // 查询卡券消息
                HighCoupon coupon = highCouponService.getCouponById(Long.parseLong(id));
                if (coupon == null) {
                    log.error("HighDiscountCouponRelController -> insertDiscountCoupon() error!","未找到卡券信息");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON, "");
                }
                // 是否存在
                if (highDiscountCouponRelService.getRelByDiscountCoupon(discountId,Long.parseLong(id)) != null) {
                    log.error("HighDiscountCouponRelController -> insertDiscountCoupon() error!","重复增加" + coupon.getCouponName());
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "重复增加" + coupon.getCouponName());
                }
                rel =new HighDiscountCouponRel();
                rel.setDiscountId(discountId);
                rel.setCouponId(Long.parseLong(id));
                rel.setStatus(1);
                rel.setCreateTime(new Date());
                list.add(rel);
            }
            highDiscountCouponRelService.insertDiscountCouponList(list);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighDiscountCouponRelController -> insertDiscountCoupon() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/delete",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "删除")
    public ResponseData delete(@RequestParam(name = "id", required = true) Long id){
        try {

            HighDiscountCouponRel rel = highDiscountCouponRelService.getRelById(id);
            if (rel == null) {
                log.error("HighDiscountCouponRelController -> delete() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            rel.setStatus(0);
            highDiscountCouponRelService.updateDiscountCoupon(rel);

            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            log.error("HighDiscountCouponRelController -> delete() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getRelByDiscount",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据优惠券 查询关联卡券")
    public ResponseData getRelByDiscount(@RequestParam(name = "discountId", required = true) Long discountId) {
        try {

            return ResponseMsgUtil.success(highDiscountCouponRelService.getRelByDiscount(discountId));

        } catch (Exception e) {
            log.error("HighDiscountController -> getRelByDiscount() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
