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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
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.system.config.BdBonusItems;
import com.hzs.common.domain.system.config.ext.BdBonusItemsExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.config.service.IBdBonusItemsService;
import com.hzs.system.config.vo.ApiBdBonusItemsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 奖项配置 前端控制器
 */
@RestController
@RequestMapping("/manage/bd-bonus-items")
public class BdBonusItemsController extends BaseController {

    @Autowired
    private IBdBonusItemsService bonusItemsService;
    @Autowired
    private RedisService redisService;

    /**
     * 查询奖项配置列表
     **/
    @Log(module = EOperationModule.BONUS_CONFIG, business = EOperationBusiness.BONUS_ITEMS, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdBonusItems bonusItems) {
        List<BdBonusItemsExt> listExt = new ArrayList<>();

        startPage();
        LambdaQueryWrapper<BdBonusItems> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdBonusItems::getPkCountry, SecurityUtils.getPkCountry());
        if (StringUtils.isNotNull(bonusItems.getBonusName())) {
            queryWrapper.eq(BdBonusItems::getBonusName, bonusItems.getBonusName());
        }
        queryWrapper.orderByAsc(BdBonusItems::getBonusValue);
        List<BdBonusItems> list = bonusItemsService.list(queryWrapper);
        list.forEach(item -> listExt.add(BeanUtil.copyProperties(item, BdBonusItemsExt.class)));

        TableDataInfo tableDataInfo = getDataTable(list);
        tableDataInfo.setRows(listExt);
        return tableDataInfo;
    }

    /**
     * 导出
     **/
    @Log(module = EOperationModule.BONUS_CONFIG, business = EOperationBusiness.BONUS_ITEMS, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdBonusItems bonusItems) {
        LambdaQueryWrapper<BdBonusItems> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdBonusItems::getPkCountry, SecurityUtils.getPkCountry());
        if (StringUtils.isNotNull(bonusItems.getBonusName())) {
            queryWrapper.eq(BdBonusItems::getBonusName, bonusItems.getBonusName());
        }
        queryWrapper.orderByAsc(BdBonusItems::getBonusValue);
        List<BdBonusItemsExt> listExt = new ArrayList<>();
        List<BdBonusItems> list = bonusItemsService.list(queryWrapper);
        list.forEach(item -> listExt.add(BeanUtil.copyProperties(item, BdBonusItemsExt.class)));
        ExcelUtil<BdBonusItemsExt> util = new ExcelUtil<>(BdBonusItemsExt.class);
        util.exportExcel(response, listExt, "奖项配置");
    }

    /**
     * 查询奖项配置列表(下拉选)
     **/
    @Log(module = EOperationModule.BONUS_CONFIG, business = EOperationBusiness.BONUS_ITEMS, method = EOperationMethod.SELECT)
    @GetMapping("/listNoPage")
    public TableDataInfo listNoPage(BdBonusItems bonusItems) {
        LambdaQueryWrapper<BdBonusItems> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdBonusItems::getPkCountry, SecurityUtils.getPkCountry());
        if (StringUtils.isNotNull(bonusItems.getBonusName())) {
            queryWrapper.eq(BdBonusItems::getBonusName, bonusItems.getBonusName());
        }
        queryWrapper.orderByAsc(BdBonusItems::getBonusValue);
        List<ApiBdBonusItemsVO> listVO = new ArrayList<>();
        List<BdBonusItems> list = bonusItemsService.list(queryWrapper);
        list.forEach(item -> listVO.add(BeanUtil.copyProperties(item, ApiBdBonusItemsVO.class)));
        return getDataTable(listVO);
    }

    /**
     * 生成奖项
     **/
    @Log(module = EOperationModule.BONUS_CONFIG, business = EOperationBusiness.BONUS_ITEMS, method = EOperationMethod.INSERT)
    @PostMapping("/generate")
    public AjaxResult generate(@RequestBody BdBonusItems bonusItems) {
        return AjaxResult.success(bonusItemsService.generate(bonusItems));
    }

    /**
     * 修改奖项配置
     **/
    @Log(module = EOperationModule.BONUS_CONFIG, business = EOperationBusiness.BONUS_ITEMS, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdBonusItems bonusItems) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isEmpty(bonusItems.getBonusName())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BONUS_NAME_NOT_NULL));
        }
        if (StringUtils.isNull(bonusItems.getEnableState())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.ENABLE_STATE_NOT_NULL));
        }
        if (StringUtils.isNull(bonusItems.getSettleType())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SETTLE_TYPE_NOT_NULL));
        }
        if (StringUtils.isNull(bonusItems.getPublishTime())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PUBLISH_TIME_NOT_NULL));
        }
        if (StringUtils.isNull(bonusItems.getPublishDate())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PUBLISH_DATE_NOT_NULL));
        }
        if (StringUtils.isNull(bonusItems.getGrantDate())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GRANT_DATE_NOT_NULL));
        }
        if (StringUtils.isNull(bonusItems.getWithdrawalDate())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.WITHDRAWAL_DATE_NOT_NULL));
        }
        bonusItems.setPkModified(SecurityUtils.getUserId());
        bonusItems.setModifiedTime(new Date());
        redisService.deleteObject(CacheConstants.BD_BONUS_ITEMS + pkCountry);
        return toAjax(bonusItemsService.updateById(bonusItems));
    }

    /**
     * 查询单条奖项配置
     **/
    @Log(module = EOperationModule.BONUS_CONFIG, business = EOperationBusiness.BONUS_ITEMS, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdBonusItems bonusItems = bonusItemsService.getById(pkId);
        BdBonusItemsExt bonusItemsExt = BeanUtil.copyProperties(bonusItems, BdBonusItemsExt.class);
        return AjaxResult.success(bonusItemsExt);
    }

    /**
     * 删除奖项配置
     **/
    @Log(module = EOperationModule.BONUS_CONFIG, business = EOperationBusiness.BONUS_ITEMS, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        LambdaUpdateWrapper<BdBonusItems> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BdBonusItems::getPkId, pkId);
        updateWrapper.set(BdBonusItems::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(BdBonusItems::getPkModified, SecurityUtils.getUserId());
        updateWrapper.set(BdBonusItems::getModifiedTime, new Date());
        redisService.deleteObject(CacheConstants.BD_BONUS_ITEMS + SecurityUtils.getPkCountry());
        return toAjax(bonusItemsService.update(updateWrapper));
    }
}
