package com.crm.manager.controller;

import com.crm.manager.service.RiskRevokeCreditRuleLoginService;
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.revoke.credit.rule.login.RiskRevokeCreditRuleLoginDto;
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.revoke.credit.rule.login.*;
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/revoke/credit/rule/login")
public class RiskRevokeCreditRuleLoginController {

    @Autowired
    private RiskRevokeCreditRuleLoginService revokeCreditRuleLoginService;
    @Autowired
    private CustomLoginFeignService customLoginFeignService;


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

        revokeCreditRuleLoginService.add(entity);

        return BaseResultDto.success();
    }

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

        revokeCreditRuleLoginService.update(entity);

        return BaseResultDto.success();
    }

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

        return BaseResultDto.success();
    }

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

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

        return result;
    }

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

        RiskRevokeCreditRuleLoginDto 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);

    }
}
