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.domain.R;
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.member.base.CuMember;
import com.hzs.common.domain.system.config.BdBonusType;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.system.config.service.IBdBonusTypeService;
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-20
 */
@Slf4j
@RestController
@RequestMapping("/manage/bonus-type")
public class BdBonusTypeController extends BaseController {

    @Autowired
    private IBdBonusTypeService bdBonusTypeService;
    @DubboReference
    private IMemberServiceApi iMemberServiceApi;

    /**
     * @description: 查询会员奖金权限配置列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [bdBonusType]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BONUS_TYPE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdBonusType bdBonusType) {
        startPage();
        QueryWrapper<BdBonusType> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(bdBonusType.getMemberCode())){
            queryWrapper.eq("MEMBER_CODE",bdBonusType.getMemberCode());
        }
        queryWrapper.orderByAsc("CREATION_TIME");
        List<BdBonusType> list = bdBonusTypeService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * @description: 新增会员奖金权限配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [bdBonusType]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BONUS_TYPE, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdBonusType bdBonusType) {
        Integer pkCountry=SecurityUtils.getPkCountry();
        bdBonusType.setPkCountry(pkCountry);
        if(StringUtils.isNull(bdBonusType.getMemberCode())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_CODE_NOT_NULL));
        }
        R<CuMember> member= iMemberServiceApi.getMember(bdBonusType.getMemberCode());
        if(StringUtils.isNull(member.getData())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID));
        }
        bdBonusType.setMemberCode(bdBonusType.getMemberCode().toUpperCase());
        if(StringUtils.isNull(bdBonusType.getPeriod())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PERIOD_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsReference())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_REFERENCE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsExpand())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_EXPAND_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCoach())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_COACH_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsShare())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_SHARE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsService())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_SERVICE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsPurchaseRange())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_PURCHASE_RANGE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsConsumeExpand())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_CONSUME_EXPAND_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsMallResale())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_MALL_RESALE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsPlatformFee())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_PLATFORM_FEE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCoupons())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_COUPONS_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsSeaBean())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_SEA_BEAN_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCloudReference())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_CLOUD_REFERENCE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCloudFirst())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_CLOUD_FIRST_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCloudRepeat())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_CLOUD_REPEAT_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsGlobalDividend())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_GLOBAL_DIVIDEND_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsGlobalIntegral())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_GLOBAL_INTEGRAL_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCarAwardPoints())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_CAR_AWARD_POINTS_NOT_NULL));
        }
        bdBonusType.setPkCountry(SecurityUtils.getPkCountry());
        bdBonusType.setPkCreator(SecurityUtils.getUserId());
        bdBonusType.setCreationTime(new Date());
        return toAjax(bdBonusTypeService.save(bdBonusType));
    }


    /**
     * @description: 修改会员奖金权限配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [bdBonusType]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BONUS_TYPE, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdBonusType bdBonusType) {
        Integer pkCountry=SecurityUtils.getPkCountry();
        bdBonusType.setPkCountry(pkCountry);
        if(StringUtils.isNull(bdBonusType.getMemberCode())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_CODE_NOT_NULL));
        }
        R<CuMember> member= iMemberServiceApi.getMember(bdBonusType.getMemberCode());
        if(StringUtils.isNull(member.getData())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID));
        }
        if(StringUtils.isNull(bdBonusType.getPeriod())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PERIOD_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsReference())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_REFERENCE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsExpand())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_EXPAND_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCoach())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_COACH_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsShare())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_SHARE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsService())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_SERVICE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsPurchaseRange())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_PURCHASE_RANGE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsConsumeExpand())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_CONSUME_EXPAND_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsMallResale())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_MALL_RESALE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsPlatformFee())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_PLATFORM_FEE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCoupons())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_COUPONS_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsSeaBean())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_SEA_BEAN_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCloudReference())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_CLOUD_REFERENCE_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCloudFirst())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_CLOUD_FIRST_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCloudRepeat())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_CLOUD_REPEAT_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsGlobalDividend())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_GLOBAL_DIVIDEND_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsGlobalIntegral())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_GLOBAL_INTEGRAL_NOT_NULL));
        }
        if(StringUtils.isNull(bdBonusType.getIsCarAwardPoints())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.IS_CAR_AWARD_POINTS_NOT_NULL));
        }
        bdBonusType.setPkModified(SecurityUtils.getUserId());
        bdBonusType.setModifiedTime(new Date());
        return toAjax(bdBonusTypeService.updateById(bdBonusType));
    }


    /**
     * @description: 查询单条会员奖金权限配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BONUS_TYPE, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdBonusType bdBonusType = bdBonusTypeService.getById(pkId);
        return AjaxResult.success(bdBonusType);
    }


    /**
     * @description: 删除会员奖金权限配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BONUS_TYPE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdBonusType> 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());
        return toAjax(bdBonusTypeService.update(updateWrapper));
    }
}
