package com.crm.manager.controller;

import com.crm.manager.service.CustomGroupService;
import com.crm.manager.service.RiskLeverageGroupService;
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.group.RiskLeverageGroupDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.login.CustomLoginSearchLoginByGroupEntity;
import com.crm.rely.backend.core.entity.risk.leverage.group.*;
import com.crm.rely.backend.core.entity.risk.leverage.group.exclude.login.RiskLeverageRuleExcludeLoginSaveEntity;
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.ArrayList;
import java.util.List;

/**
 * 自动杠杠修改 组别设置
 *
 * @author houn
 */
@RestController
@RequestMapping("/risk/leverage/group")
public class RiskLeverageGroupController {

    @Autowired
    private RiskLeverageGroupService riskLeverageGroupService;

    @Autowired
    private CustomGroupService customGroupService;

    @Autowired
    private CustomLoginFeignService customLoginFeignService;

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

        List<RiskLeverageRuleExcludeLoginSaveEntity> excludeLoginSaveEntities
                = getRiskLeverageRuleExcludeLoginSaveEntity(entity.getPlatform(), entity.getExcludeLogins());

        entity.setExcludeLoginSaveEntities(excludeLoginSaveEntities);

        riskLeverageGroupService.add(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/update")
    public BaseResultDto update(@RequestBody @Validated RiskLeverageGroupUpdateEntity entity) throws Exception {
        List<RiskLeverageRuleExcludeLoginSaveEntity> excludeLoginSaveEntities
                = getRiskLeverageRuleExcludeLoginSaveEntity(entity.getPlatform(), entity.getExcludeLogins());

        entity.setExcludeLoginSaveEntities(excludeLoginSaveEntities);

        riskLeverageGroupService.update(entity);

        return BaseResultDto.success();
    }

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

        return BaseResultDto.success();
    }

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

       
        ResultWithPagerDto<RiskLeverageGroupDto> result = riskLeverageGroupService.searchPageList(entity);

        return result;
    }

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

        RiskLeverageGroupDto riskRevokeCreditRuleGroupDto = riskLeverageGroupService.searchSingle(entity);

        return BaseResultDto.success(riskRevokeCreditRuleGroupDto);
    }


    @PostMapping("/{method}/group/list")
    public BaseResultDto<List<String>> searchGroupList(@PathVariable("method") String method,
                                                       @RequestBody @Validated RiskLeverageGroupSearchGroupByPlatformEntity entity) throws Exception {
        if (!"add".equals(method) && !"update".equals(method)) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        List<String> groups = customGroupService.searchGroupList(entity.getPlatform());
        return BaseResultDto.success(groups);
    }

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

    }

    private List<RiskLeverageRuleExcludeLoginSaveEntity> getRiskLeverageRuleExcludeLoginSaveEntity(String platform,
                                                                                                   List<Long> excludeLogins) {
        if (excludeLogins == null || excludeLogins.size() <= 0) {
            return null;
        }
        List<RiskLeverageRuleExcludeLoginSaveEntity> excludeLoginSaveEntities = new ArrayList<>(excludeLogins.size());
        for (Long login : excludeLogins) {
            RiskLeverageRuleExcludeLoginSaveEntity riskLeverageRuleExcludeLoginSaveEntity =
                    new RiskLeverageRuleExcludeLoginSaveEntity();
            riskLeverageRuleExcludeLoginSaveEntity.setPlatform(platform);
            riskLeverageRuleExcludeLoginSaveEntity.setLogin(login);
            excludeLoginSaveEntities.add(riskLeverageRuleExcludeLoginSaveEntity);
        }
        return excludeLoginSaveEntities;
    }
}
