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.constant.CacheConstants;
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.service.RedisService;
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.BdTransferSpecial;
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.IBdTransferSpecialService;
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-07
 */
@Slf4j
@RestController
@RequestMapping("/manage/transfer-special")
public class BdTransferSpecialController extends BaseController {

    @Autowired
    private IBdTransferSpecialService transferSpecialService;
    @DubboReference
    private IMemberServiceApi iMemberServiceApi;
    private RedisService redisService;

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

    /**
     * @description: 查询转账特殊设置表列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: transferSpecial
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("/list")
    public TableDataInfo list(BdTransferSpecial transferSpecial) {
        startPage();
        QueryWrapper<BdTransferSpecial> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(transferSpecial.getPkTransfer())){
            queryWrapper.eq("PK_TRANSFER",transferSpecial.getPkTransfer());
        }
        if(StringUtils.isNotNull(transferSpecial.getPkMember())){
            queryWrapper.eq("MEMBER_CODE",transferSpecial.getPkMember());
        }
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdTransferSpecial> list = transferSpecialService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * @description: 新增转账特殊设置表
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: transferSpecial
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdTransferSpecial transferSpecial) {
        if(StringUtils.isNull(transferSpecial.getPkMember())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_CODE_NOT_NULL));
        }
        R<CuMember> member= iMemberServiceApi.getMember(transferSpecial.getPkMember());
        if(StringUtils.isNull(member.getData())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID));
        }
        transferSpecial.setPkMember(transferSpecial.getPkMember());
        if(StringUtils.isNull(transferSpecial.getSpecialConfig())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TRANSFER_SPECIAL_MIN_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(transferSpecial.getMinAmountMoney())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MIN_AMOUNT_MONEY_NOT_NULL));
        }
        transferSpecial.setPkCountry(SecurityUtils.getPkCountry());
        transferSpecial.setPkCreator(SecurityUtils.getUserId());
        transferSpecial.setCreationTime(new Date());
        redisService.deleteObject(CacheConstants.BD_TRANSFER_SPECIAL + SecurityUtils.getPkCountry());
        return toAjax(transferSpecialService.save(transferSpecial));
    }


    /**
     * @description: 修改转账特殊设置表
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: transferSpecial
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdTransferSpecial transferSpecial) {
        if(StringUtils.isNull(transferSpecial.getPkMember())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_CODE_NOT_NULL));
        }
        transferSpecial.setPkMember(transferSpecial.getPkMember());
        R<CuMember> member= iMemberServiceApi.getMember(transferSpecial.getPkMember());
        if(StringUtils.isNull(member.getData())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MEMBER_NOT_VALID));
        }
        if(StringUtils.isNull(transferSpecial.getSpecialConfig())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TRANSFER_SPECIAL_MIN_AMOUNT_NOT_NULL));
        }
        if(StringUtils.isNull(transferSpecial.getMinAmountMoney())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.MIN_AMOUNT_MONEY_NOT_NULL));
        }
        transferSpecial.setPkModified(SecurityUtils.getUserId());
        transferSpecial.setModifiedTime(new Date());
        redisService.deleteObject(CacheConstants.BD_TRANSFER_SPECIAL + SecurityUtils.getPkCountry());
        return toAjax(transferSpecialService.updateById(transferSpecial));
    }


    /**
     * @description: 查询单条转账特殊设置表
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdTransferSpecial transferSpecial = transferSpecialService.getById(pkId);
        return AjaxResult.success(transferSpecial);
    }


    /**
     * @description: 删除转账特殊设置表
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdTransferSpecial> 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_TRANSFER_SPECIAL + SecurityUtils.getPkCountry());
        return toAjax(transferSpecialService.update(updateWrapper));
    }
}
