package com.crm.manager.controller;

import com.crm.manager.service.RiskLeverageLoginService;
import com.crm.manager.service.custom.CustomLoginFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.risk.leverage.login.RiskLeverageLoginDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.login.CustomLoginSearchLoginByPlatformEntity;
import com.crm.rely.backend.core.entity.risk.leverage.login.RiskLeverageLoginAddEntity;
import com.crm.rely.backend.core.entity.risk.leverage.login.RiskLeverageLoginDeleteEntity;
import com.crm.rely.backend.core.entity.risk.leverage.login.RiskLeverageLoginSearchPageEntity;
import com.crm.rely.backend.core.entity.risk.leverage.login.RiskLeverageLoginUpdateEntity;
import com.crm.rely.backend.core.entity.risk.revoke.credit.rule.login.RiskRevokeCreditRuleGroupSearchLoginByPlatformEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 自动杠杠修改 账号设置
 *
 * @author houn
 */
@RestController
@RequestMapping("/risk/leverage/login")
public class RiskLeverageLoginController {

    @Autowired
    private RiskLeverageLoginService revokeCreditRuleLoginService;
    @Autowired
    private CustomLoginFeignService customLoginFeignService;


    /**
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    public BaseResultDto add(@RequestBody @Validated RiskLeverageLoginAddEntity entity) throws Exception {

        revokeCreditRuleLoginService.add(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated RiskLeverageLoginUpdateEntity entity) throws Exception {

        revokeCreditRuleLoginService.update(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated RiskLeverageLoginDeleteEntity entity) throws Exception {
        revokeCreditRuleLoginService.delete(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<RiskLeverageLoginDto> pageList(@RequestBody @Validated RiskLeverageLoginSearchPageEntity entity) throws Exception {

        ResultWithPagerDto<RiskLeverageLoginDto> result = revokeCreditRuleLoginService.searchPageList(entity);

        return result;
    }

    @PostMapping("/search/single")
    public BaseResultDto<RiskLeverageLoginDto> single(@RequestBody @Validated SingleLongEntity entity) throws Exception {

        RiskLeverageLoginDto riskRevokeCreditRuleGroupDto = revokeCreditRuleLoginService.searchSingle(entity);

        return BaseResultDto.success(riskRevokeCreditRuleGroupDto);
    }

    @PostMapping("/{method}/login/list")
    public BaseResultDto<List<Long>> searchLoginList(@PathVariable("method") String method,
                                                     @RequestBody @Validated RiskRevokeCreditRuleGroupSearchLoginByPlatformEntity entity) throws Exception {
        if (!"add".equals(method) && !"update".equals(method)) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        CustomLoginSearchLoginByPlatformEntity customLoginSearchLoginByPlatformEntity = new CustomLoginSearchLoginByPlatformEntity();
        BeanUtils.copyProperties(entity, customLoginSearchLoginByPlatformEntity);
        return customLoginFeignService.managerSearchPlatformLoginList(customLoginSearchLoginByPlatformEntity);

    }
}
