package com.hzs.system.bonus.controller.manage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.EServiceType;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.bonus.BdBonusService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.bonus.params.BonusServiceParams;
import com.hzs.system.bonus.service.IBdBonusServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: yuhui
 * @Time: 2022/11/1 16:19
 * @Classname: BdBonusServiceController
 * @PackageName: com.hzs.sale.bonus.controller.manage
 */
@RestController
@RequestMapping("/manage/bonus-service")
public class BdBonusServiceController extends BaseController {

    @Autowired
    private IBdBonusServiceService bonusServiceService;
    @Autowired
    private RedisService redisService;

    /**
     * 服务补贴列表
     */
    @GetMapping("list")
    public TableDataInfo list() {
        Integer pkCounrey = SecurityUtils.getPkCountry();
        QueryWrapper<BdBonusService> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", pkCounrey);
        List<BdBonusService> list = bonusServiceService.list(queryWrapper);
        List<Integer> typeList = new ArrayList<>();
        // 如果没有值则添加新值
        if (list.size() != EServiceType.values().length) {
            for (BdBonusService bdBonusService : list) {
                typeList.add(bdBonusService.getServiceType());
            }
            for (EServiceType value : EServiceType.values()) {
                if (!typeList.contains(value.getValue())) {
                    BdBonusService bonusService = new BdBonusService();
                    bonusService.setServiceType(value.getValue());
                    bonusService.setServiceRadio(BigDecimal.ZERO);
                    bonusService.setPkCountry(pkCounrey);
                    bonusService.setPkCreator(SecurityUtils.getUserId());
                    bonusService.setCreationTime(new Date());
                    bonusServiceService.save(bonusService);
                    list.add(bonusService);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 新增服务补贴
     *
     * @param bdBonusService
     * @return
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody BdBonusService bdBonusService) {
        bdBonusService.setPkCreator(SecurityUtils.getUserId());
        bdBonusService.setCreationTime(new Date());
        bdBonusService.setPkCountry(SecurityUtils.getPkCountry());
        bonusServiceService.save(bdBonusService);
        return AjaxResult.success();
    }

    /**
     * 修改服务补贴
     *
     * @return
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody BonusServiceParams BonusServiceParams) {
        for (BdBonusService bdBonusService : BonusServiceParams.getBdBonusServiceList()) {
            bdBonusService.setPkModified(SecurityUtils.getUserId());
            bdBonusService.setModifiedTime(new Date());
            String redisKey = CacheConstants.BD_BONUS_SERVICE + bdBonusService.getPkId();
            redisService.deleteObject(redisKey);
        }
        return AjaxResult.success(bonusServiceService.updateBatchById(BonusServiceParams.getBdBonusServiceList()));
    }

    /**
     * 删除服务补贴
     *
     * @return
     */
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Integer pkId) {
        UpdateWrapper<BdBonusService> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", pkId);
        updateWrapper.set("MODIFIED_TIME", new Date());
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        bonusServiceService.update(updateWrapper);
        String redisKey = CacheConstants.BD_BONUS_SERVICE + pkId;
        redisService.deleteObject(redisKey);
        return AjaxResult.success();
    }

    /**
     * 服务补贴新增
     *
     * @return
     */
    @GetMapping("detail")
    public AjaxResult detail(Integer pkId) {
        return AjaxResult.success(bonusServiceService.getBonusServiceById(pkId));
    }
}
