package com.hzs.bonus.risk.controller.manage;/**
 * @Description:
 * @Author: yuhui
 * @Time: 2024/4/9 16:55
 * @Classname: MemberRiskControlController
 * @PackageName: com.hzs.bonus.risk.controller.manage
 */

import cn.hutool.core.bean.BeanUtil;
import com.hzs.bonus.risk.param.MemberRiskControlParam;
import com.hzs.bonus.risk.param.MemberRiskParam;
import com.hzs.bonus.risk.service.ICuMemberRiskControlService;
import com.hzs.bonus.risk.vo.MemberRiskControlVo;
import com.hzs.common.core.constant.msg.MemberMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.DateUtils;
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.bonus.risk.CuMemberRiskControl;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.security.service.UserTokenService;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.base.IMemberServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *@BelongsProject: hzs_cloud
 *@BelongsPackage: com.hzs.bonus.risk.controller.manage
 *@Author: yh
 *@CreateTime: 2024-04-09  16:55
 *@Description: TODO
 *@Version: 1.0
 */
@RestController
@RequestMapping("/manage/member-risk-control")
public class MemberRiskControlController extends BaseController {
    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private ICuMemberRiskControlService memberRiskControlService;
    @DubboReference
    private IMemberServiceApi memberServiceApi;
    /**
     * 查询列表
     * @param memberRiskControlParam
     * @return
     */
    @GetMapping("list")
    public TableDataInfo list(MemberRiskControlParam memberRiskControlParam){
        startPage();
        if (memberRiskControlParam.getBeginStartDate() != null){
            memberRiskControlParam.setBeginStartDate(DateUtils.getStartTime(memberRiskControlParam.getBeginStartDate()));
        }
        if (memberRiskControlParam.getEndStartDate() != null){
            memberRiskControlParam.setEndStartDate(DateUtils.getEndTime(memberRiskControlParam.getEndStartDate()));
        }
        if (memberRiskControlParam.getBeginEndDate() != null){
            memberRiskControlParam.setBeginEndDate(DateUtils.getStartTime(memberRiskControlParam.getBeginEndDate()));
        }
        if (memberRiskControlParam.getEndEndDate() != null){
            memberRiskControlParam.setEndEndDate(DateUtils.getEndTime(memberRiskControlParam.getEndEndDate()));
        }
        memberRiskControlParam.setPkCountry(SecurityUtils.getPkCountry());
        List<MemberRiskControlVo> memberRiskControlVoList = memberRiskControlService.queryByList(memberRiskControlParam);


        return getDataTable(memberRiskControlVoList);
    }


    /**
     * 新增
     * @param memberRiskControlParam
     * @return
     */
    @PostMapping("save")
    public AjaxResult save(@RequestBody MemberRiskControlParam memberRiskControlParam){
        R<CuMember> memberDto = memberServiceApi.getMember(memberRiskControlParam.getMemberCode());
        CuMember member = memberDto.getData();
        if (member == null){
            return AjaxResult.error(MemberMsgConstants.MEMBER_NOT_EXITS);
        }
        CuMemberRiskControl memberRiskControl = BeanUtil.copyProperties(memberRiskControlParam, CuMemberRiskControl.class);
        memberRiskControl.setPkMember(member.getPkId());
        memberRiskControl.setPkCountry(SecurityUtils.getPkCountry());
        memberRiskControl.setCreationTime(DateUtils.currentDateTime());
        memberRiskControl.setPkCreator(SecurityUtils.getUserId());
        memberRiskControlService.save(memberRiskControl);
        return AjaxResult.success();
    }

    /**
     * 修改
     * @param memberRiskControlParam
     * @return
     */
    @PostMapping("update")
    public AjaxResult update(@RequestBody MemberRiskControlParam memberRiskControlParam){
        R<CuMember> memberDto = memberServiceApi.getMember(memberRiskControlParam.getMemberCode());
        CuMember member = memberDto.getData();
        if (member == null){
            return AjaxResult.error(MemberMsgConstants.MEMBER_NOT_EXITS);
        }
        CuMemberRiskControl memberRiskControl = BeanUtil.copyProperties(memberRiskControlParam, CuMemberRiskControl.class);
        memberRiskControl.setPkModified(SecurityUtils.getUserId());
        memberRiskControl.setModifiedTime(DateUtils.currentDateTime());
        memberRiskControlService.updateById(memberRiskControl);
        return AjaxResult.success();
    }

    /**
     * 删除
     * @param pkMemberRiskControl
     * @return
     */
    @GetMapping("remove")
    public AjaxResult remove(Long pkMemberRiskControl){
        return AjaxResult.success(memberRiskControlService.removeById(pkMemberRiskControl));
    }

    /**
     * 详情
     * @param pkMemberRiskControl
     * @return
     */
    @GetMapping("detail")
    public AjaxResult detail(Long pkMemberRiskControl){
        CuMemberRiskControl memberRiskControl = memberRiskControlService.getById(pkMemberRiskControl);
        R<CuMember> memberDto = memberServiceApi.getMember(memberRiskControl.getPkMember());
        CuMember member = memberDto.getData();
        MemberRiskControlVo memberRiskControlVo = BeanUtil.copyProperties(memberRiskControl, MemberRiskControlVo.class);
        memberRiskControlVo.setMemberCode(member.getMemberCode());
        return AjaxResult.success(memberRiskControlVo);
    }


    /**
     * k值风控设置  审批
     * @return
     */
    @PostMapping("/online-petition")
    public AjaxResult onLinePetition(@RequestBody MemberRiskParam memberRiskParam){
        memberRiskParam.setPkCountry(SecurityUtils.getPkCountry());
        memberRiskParam.setPkMember(SecurityUtils.getUserId());
        memberRiskParam.setLoginUser(userTokenService.getLoginUser());

        memberRiskControlService.onLinePetition(memberRiskParam);
        return AjaxResult.success();
    }
}
