package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.thmz.Discount;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.service.thmz.DiscountService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class DiscountController {
    @Autowired
    private DiscountService discountService;

    /**
     * 分页查询收费项目惠民活动列表
     * @param discount 收费项目查询条件
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/listDiscount", method = {RequestMethod.POST})
    public Map<String, Object> listDiscount(@RequestBody Discount discount) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(discount==null){
                resultMap.put("code", -1);
                resultMap.put("message", "查询营惠民活动列表失败，参数为空");
                return resultMap;
            }
            if(StringUtils.isBlank(discount.getDeptNo())){
                discount.setDeptNo(null);
            }
            if(StringUtils.isBlank(discount.getChargeItemCodes())){
                discount.setChargeItemCodes(null);
            }
            if(StringUtils.isBlank(discount.getBeginTime())){
                discount.setBeginTime(null);
            }
            if(StringUtils.isBlank(discount.getEndTime())){
                discount.setEndTime(null);
            }
            int total = discountService.queryDiscount(discount);
            List<Discount> list= discountService.queryDiscountWithPage(discount);
            resultMap.put("code", 0);
            resultMap.put("message", "查询惠民活动列表成功");
            resultMap.put("total", total);
            resultMap.put("data", list);
            return resultMap;
        }catch (Exception e){
            log.error(e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询惠民活动列表失败");
            return resultMap;
        }
    }


    /**
     * 按照主键查询惠民活动计划
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getDiscountById", method = {RequestMethod.GET})
    public Map<String, Object> getDiscountById(@RequestParam("id") Long id) {
        Map<String, Object> resultMap = new HashMap<>();
        if(id==null){
            resultMap.put("code", -1);
            resultMap.put("message", "主键不能为空");
            return resultMap;
        }
        try {
            Discount discount=discountService.queryById(id);
            if(discount==null){
                resultMap.put("code", -1);
                resultMap.put("message", "惠民活动不存在");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询惠民活动成功");
            resultMap.put("data", discount);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询惠民活动失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "查询惠民活动失败");
            return resultMap;
        }
    }



    /**
     * 停用或者启用惠民活动
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/updateDiscountDelFlag", method = {RequestMethod.GET})
    public Map<String, Object> updateDiscountDelFlag(@RequestParam("id") Long id,@RequestParam("delFlag") Integer delFlag, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        if(id==null){
            resultMap.put("code", -1);
            resultMap.put("message", "主键不能为空");
            return resultMap;
        }
        if(!YesNoEnum.YES.code.equals(delFlag) && !YesNoEnum.NO.code.equals(delFlag)){
            resultMap.put("code", -1);
            resultMap.put("message", "停用标志错误");
            return resultMap;
        }
        String str = YesNoEnum.YES.code.equals(delFlag)?"停用":"启用";
        try {
            Discount discount = new Discount();
            discount.setId(id);
            discount.setDeleteFlag(delFlag);
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            discount.setUpdateId(tokenUser.getUserIdCode());
            int num=discountService.modify(discount);
            if(num==0){
                resultMap.put("code", -1);
                resultMap.put("message", str+"失败");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", str+"惠民活动成功");
            resultMap.put("data", num);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(str+"惠民活动失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", str+"惠民活动失败");
            return resultMap;
        }
    }


    /**
     * 保存惠民活动
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/saveDiscount", method = {RequestMethod.POST})
    public Map<String, Object> saveDiscount(@RequestBody Discount discount, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if(discount==null){
                resultMap.put("code", -1);
                resultMap.put("message", "参数不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(discount.getName())){
                resultMap.put("code", -1);
                resultMap.put("message", "惠民活动名称不能为空");
                return resultMap;
            }
            if(discount.getDeptType()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "科室类型不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(discount.getDeptNo())){
                resultMap.put("code", -1);
                resultMap.put("message", "科室编码不能为空");
                return resultMap;
            }
            if(discount.getDiscountBeginTime()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "惠民活动开始时间不能为空");
                return resultMap;
            }
            if(discount.getDiscountEndTime()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "惠民活动结束时间不能为空");
                return resultMap;
            }
            if(StringUtils.isBlank(discount.getChargeItemCodes())){
                resultMap.put("code", -1);
                resultMap.put("message", "项目编码不能为空");
                return resultMap;
            }
            if(discount.getDiscountType()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "优惠类型不能为空");
                return resultMap;
            }
            if(!Arrays.asList(YesNoEnum.YES.code,YesNoEnum.NO.code).contains(discount.getDiscountType())){
                resultMap.put("code", -1);
                resultMap.put("message", "优惠类型值超出范围");
                return resultMap;
            }
            if(YesNoEnum.YES.code.equals(discount.getDiscountType()) && discount.getDiscountRate()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "折扣率不能为空");
                return resultMap;
            }
            if(YesNoEnum.NO.code.equals(discount.getDiscountType()) && discount.getDiscountPrice()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "优惠价不能为空");
                return resultMap;
            }
            if(discount.getDeleteFlag()==null){
                resultMap.put("code", -1);
                resultMap.put("message", "停用标志不能为空");
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            if(discount.getId()==null){
                discount.setCreateId(tokenUser.getUserIdCode());
                //新增
                int num=discountService.save(discount);
                if(num==0){
                    resultMap.put("code", -1);
                    resultMap.put("message", "保存失败，请重新操作或者联系管理员");
                    return resultMap;
                }
            }else {
                discount.setUpdateId(tokenUser.getUserIdCode());
                //修改
                discountService.modify(discount);
            }
            resultMap.put("code", 0);
            resultMap.put("message", "保存惠民活动成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存惠民活动失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "保存惠民活动失败");
            return resultMap;
        }
    }



    /**
     * 删除主键查询惠民活动
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/removeById", method = {RequestMethod.GET})
    public Map<String, Object> removeById(@RequestParam("id") Long id) {
        Map<String, Object> resultMap = new HashMap<>();
        if(id==null){
            resultMap.put("code", -1);
            resultMap.put("message", "主键不能为空");
            return resultMap;
        }
        try {
            int num=discountService.removeById(id);
            if(num==0){
                resultMap.put("code", -1);
                resultMap.put("message", "删除营惠民活动失败");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "删除惠民活动成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除惠民活动失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "删除惠民活动失败");
            return resultMap;
        }
    }
}
