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


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.domain.R;
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.core.utils.TransactionUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.service.IBdBonusItemsService;
import com.hzs.system.config.vo.ApiBdBonusItemsVO;
import org.apache.dubbo.config.annotation.DubboReference;
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;

/**
 * <p>
 * 奖项配置 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-11-09
 */
@RestController
@RequestMapping("/manage/bd-bonus-items")
public class BdBonusItemsController extends BaseController {

    @Autowired
    private IBdBonusItemsService bonusItemsService;
    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;
    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }


    /**
     * @description: 查询奖项配置列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [bonusItems]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BONUS_CONFIG, business=EOperationBusiness.BONUS_ITEMS, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdBonusItems bonusItems) {
        startPage();
        QueryWrapper<BdBonusItems> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(bonusItems.getBonusName())){
            queryWrapper.eq("BONUS_NAME",bonusItems.getBonusName());
        }
        queryWrapper.orderByAsc("PK_ID");
        List<BdBonusItemsExt> listExt = new ArrayList<>();
        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;
    }

    /**
     * @description: 导出
     * @author: zhang jing
     * @date: 2023/4/4 9:56
     * @param: [response, bonusItems]
     * @return: void
     **/
    @Log(module = EOperationModule.BONUS_CONFIG, business=EOperationBusiness.BONUS_ITEMS, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdBonusItems bonusItems) {
        QueryWrapper<BdBonusItems> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(bonusItems.getBonusName())){
            queryWrapper.eq("BONUS_NAME",bonusItems.getBonusName());
        }
        queryWrapper.orderByAsc("PK_ID");
        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, "奖项配置");
    }
    /**
     * @description: 查询奖项配置列表(下拉选)
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [bonusItems]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BONUS_CONFIG, business=EOperationBusiness.BONUS_ITEMS, method = EOperationMethod.SELECT)
    @GetMapping("/listNoPage")
    public TableDataInfo listNoPage(BdBonusItems bonusItems) {
        QueryWrapper<BdBonusItems> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(bonusItems.getBonusName())){
            queryWrapper.eq("BONUS_NAME",bonusItems.getBonusName());
        }
        queryWrapper.orderByAsc("PK_ID");
        List<ApiBdBonusItemsVO> listVO = new ArrayList<>();
        List<BdBonusItems> list = bonusItemsService.list(queryWrapper);
        list.forEach(item -> listVO.add(BeanUtil.copyProperties(item, ApiBdBonusItemsVO.class)));
        return getDataTable(listVO);
    }

    /**
     * @description: 生成奖项
     * @author: zhang jing
     * @date: 2022/10/22 9:54
     * @param: [grade]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @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));
    }


    /**
     * @description: 修改奖项配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [bonusItems]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @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));
        }


        //字段翻译保存到数据库
        if(null!=bonusItems.getPkTransaction()){
            R<Boolean> b = iTransactionServiceApi.updateTransaction(pkCountry,
                    bonusItems.getBonusName(),bonusItems.getPkTransaction(), true);
        }else{
            //字段翻译保存到数据库
            R<Integer> pkTransaction= iTransactionServiceApi.createTransaction(pkCountry, ETransactionKey.BD_BONUS_ITEMS,
                    bonusItems.getBonusName(), EYesNo.YES, EYesNo.NO);
            bonusItems.setPkTransaction(pkTransaction.getData());
        }
        bonusItems.setPkModified(SecurityUtils.getUserId());
        bonusItems.setModifiedTime(new Date());
        redisService.deleteObject(CacheConstants.BD_BONUS_ITEMS + pkCountry);
        return toAjax(bonusItemsService.updateById(bonusItems));
    }





    /**
     * @description: 查询单条奖项配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @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);
    }


    /**
     * @description: 删除奖项配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BONUS_CONFIG, business=EOperationBusiness.BONUS_ITEMS, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdBonusItems> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", pkId);
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("MODIFIED_TIME", new Date());
        redisService.deleteObject(CacheConstants.BD_BONUS_ITEMS + SecurityUtils.getPkCountry());
        //删除翻译
        BdBonusItems bdBonusItems=bonusItemsService.getById(pkId);
        List<Long> pkIdList=new ArrayList<>();
        if(null!=bdBonusItems.getPkTransaction()){
            pkIdList.add(bdBonusItems.getPkTransaction().longValue());
            R<Boolean> b=iTransactionServiceApi.removeTransactionByPkId(pkIdList);
        }
        return toAjax(bonusItemsService.update(updateWrapper));
    }
}
