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


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.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.StringUtils;
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.BdShareDetail;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.service.IBdShareDetailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 分红配置明细表 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-09-26
 */
@Slf4j
@RestController
@RequestMapping("/manage/share-detail")
public class BdShareDetailController extends BaseController {

    @Autowired
    private IBdShareDetailService shareDetailService;
    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;

    /**
     * @description: 查询分红配置明细列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [shareDetail]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.SHARE_DETAIL, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdShareDetail shareDetail) {
        startPage();
        QueryWrapper<BdShareDetail> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(shareDetail.getPkShare())){
            queryWrapper.eq("PK_SHARE",shareDetail.getPkShare());
        }
        if(StringUtils.isNotNull(shareDetail.getPkAwards())){
            queryWrapper.eq("PK_AWARDS",shareDetail.getPkAwards());
        }
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdShareDetail> list = shareDetailService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * @description: 新增分红配置明细
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [shareDetail]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SHARE_DETAIL, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdShareDetail shareDetail) {
        Integer pkCountry=SecurityUtils.getPkCountry();
        if(StringUtils.isNull(shareDetail.getPkShare())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_SHARE_NOT_NULL));
        }
        if(StringUtils.isNull(shareDetail.getPkAwards())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.AWARDS_NAME_NOT_NULL));
        }
        if(StringUtils.isNull(shareDetail.getTotalNumber())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TOTAL_NUMBER_NOT_NULL));
        }
        if(StringUtils.isNull(shareDetail.getReachNumber())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.REACH_NUMBER_NOT_NULL));
        }
        if(StringUtils.isNull(shareDetail.getAdvanceMoney())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.ADVANCE_MONEY_NOT_NULL));
        }
        if(StringUtils.isNull(shareDetail.getRatio())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.RATIO_NOT_NULL));
        }
        if(StringUtils.isNull(shareDetail.getPayMoney())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PAY_MONEY_NOT_NULL));
        }

        shareDetail.setPkCountry(pkCountry);
        shareDetail.setPkCreator(SecurityUtils.getUserId());
        shareDetail.setCreationTime(new Date());
        return toAjax(shareDetailService.save(shareDetail));
    }


    /**
     * @description: 修改分红配置明细
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [shareDetail]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SHARE_DETAIL, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdShareDetail shareDetail) {
        Integer pkCountry=SecurityUtils.getPkCountry();
        if(StringUtils.isNull(shareDetail.getPkShare())){
            return AjaxResult.error(ConfigMsgConstants.PK_SHARE_NOT_NULL);
        }
        if(StringUtils.isNull(shareDetail.getPkAwards())){
            return AjaxResult.error(ConfigMsgConstants.AWARDS_NAME_NOT_NULL);
        }
        if(StringUtils.isNull(shareDetail.getTotalNumber())){
            return AjaxResult.error(ConfigMsgConstants.TOTAL_NUMBER_NOT_NULL);
        }
        if(StringUtils.isNull(shareDetail.getReachNumber())){
            return AjaxResult.error(ConfigMsgConstants.REACH_NUMBER_NOT_NULL);
        }
        if(StringUtils.isNull(shareDetail.getAdvanceMoney())){
            return AjaxResult.error(ConfigMsgConstants.ADVANCE_MONEY_NOT_NULL);
        }
        if(StringUtils.isNull(shareDetail.getRatio())){
            return AjaxResult.error(ConfigMsgConstants.RATIO_NOT_NULL);
        }
        if(StringUtils.isNull(shareDetail.getPayMoney())){
            return AjaxResult.error(ConfigMsgConstants.PAY_MONEY_NOT_NULL);
        }
        shareDetail.setPkCountry(pkCountry);
        shareDetail.setPkModified(SecurityUtils.getUserId());
        shareDetail.setModifiedTime(new Date());
        return toAjax(shareDetailService.updateById(shareDetail));
    }


    /**
     * @description: 查询单条分红配置明细
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SHARE_DETAIL, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdShareDetail shareDetail = shareDetailService.getById(pkId);
        return AjaxResult.success(shareDetail);
    }


    /**
     * @description: 删除分红配置明细
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.SHARE_DETAIL, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdShareDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", pkId);
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("MODIFIED", SecurityUtils.getUsername());
        updateWrapper.set("MODIFIED_TIME", new Date());
        return toAjax(shareDetailService.update(updateWrapper));
    }
}
