package thyyxxk.webserver.controller.outpatient.coupon;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.entity.outpatient.coupon.CouponGainWay;
import thyyxxk.webserver.entity.outpatient.coupon.CouponInquiry;
import thyyxxk.webserver.entity.outpatient.coupon.CouponType;
import thyyxxk.webserver.entity.outpatient.coupon.ZdCoupon;
import thyyxxk.webserver.service.outpatient.coupon.CouponManageService;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.util.List;

@RestController
@RequestMapping("/couponManagement")
public class CouponManageController {
    private final CouponManageService service;

    @Autowired
    public CouponManageController(CouponManageService service) {
        this.service = service;
    }

    @PostMapping("/getAllCoupons")
    public ResultVo<List<ZdCoupon>> getAllCoupons(@RequestBody CouponInquiry inquiry) {
        List<ZdCoupon> list = service.getAllCoupons(inquiry);
        if (list.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(list);
    }

    @PostMapping("/updateCouponState")
    public ResultVo<String> updateCouponState(@RequestBody ZdCoupon coupon) {
        if (null != coupon.getTotalQuantity() && null != coupon.getLeftQuantity()) {
            if (coupon.getLeftQuantity() > coupon.getTotalQuantity()) {
                return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR,
                        "总量不能小于余量！");
            }
        }
        if (coupon.getGainWay() == CouponGainWay.PURCHASE) {
            if (coupon.getPrice() <= 0) {
                return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "优惠券单价必须大于0");
            }
            coupon.setEffectiveDays(365 * 100);
        }
        String response = service.updateCouponState(coupon);
        return ResultVoUtil.success(response);
    }

    @GetMapping("/getSalesmanInfo")
    public ResultVo<UserInfo> getSalesmanInfo(@RequestParam("codeRs") String codeRs) {
        UserInfo userInfo = service.getSalesmanByCodeRs(codeRs);
        if (null == userInfo) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST,
                    "没有此工号的在职员工信息，请重新输入！");
        }
        return ResultVoUtil.success(userInfo);
    }

    @PostMapping("/insertNewCoupon")
    public ResultVo<String> insertNewCoupon(@RequestBody ZdCoupon coupon) {
        if (StringUtil.isBlank(coupon.getName())) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "优惠券名称不能为空。");
        }
        if (StringUtil.notBlank(coupon.getAvailableCharge()) &&
                StringUtil.isBlank(coupon.getDescription())) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "优惠券描述不能为空。");
        }
        if (null == coupon.getGainWay()) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "优惠券获取方式不能为空。");
        }
        if (null == coupon.getType()) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "优惠券下发方式不能为空。");
        }
        if (null == coupon.getPrice()) {
            coupon.setPrice(0);
        }
        if (coupon.getGainWay() == CouponGainWay.PURCHASE) {
            if (coupon.getPrice() <= 0) {
                return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "优惠券单价必须大于0");
            }
            if (coupon.getType() != CouponType.SALESMAN_OPERATION) {
                return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "【购买获取】的优惠券下发方式必须为【医院运营】。");
            }
            coupon.setEffectiveDays(365 * 100);
        }
        if (null == coupon.getValue()) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "优惠券金额不能为空。");
        }
        if (null == coupon.getTotalQuantity()) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "优惠券总量不能为空。");
        }
        if (null == coupon.getEffectiveDays()) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "优惠券有效天数不能为空。");
        }
        String response = service.insertNewCoupon(coupon);
        if (response.equals("SUCCESS")) {
            return ResultVoUtil.success("新增优惠券成功。");
        }
        return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, response);
    }
}
