package com.crm.manager.controller;

import com.crm.manager.service.CustomGroupService;
import com.crm.manager.service.RiskRevokeCreditRuleGroupService;
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.group.RiskRevokeCreditRuleGroupDto;
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.revoke.credit.rule.exclude.login.RiskRevokeCreditRuleExcludeLoginSaveEntity;
import com.crm.rely.backend.core.entity.risk.revoke.credit.rule.group.*;
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/revoke/credit/rule/group")
public class RiskRevokeCreditRuleGroupController {

    @Autowired
    private RiskRevokeCreditRuleGroupService riskRevokeCreditRuleGroupService;

    @Autowired
    private CustomGroupService customGroupService;

    @Autowired
    private CustomLoginFeignService customLoginFeignService;

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

        List<RiskRevokeCreditRuleExcludeLoginSaveEntity> excludeLoginSaveEntities
                = getRiskRevokeCreditRuleExcludeLoginSaveEntity(entity.getPlatform(), entity.getExcludeLogins());

        entity.setExcludeLoginSaveEntities(excludeLoginSaveEntities);

        riskRevokeCreditRuleGroupService.add(entity);

        return BaseResultDto.success();
    }

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

        entity.setExcludeLoginSaveEntities(excludeLoginSaveEntities);

        riskRevokeCreditRuleGroupService.update(entity);

        return BaseResultDto.success();
    }

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

        return BaseResultDto.success();
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<RiskRevokeCreditRuleGroupDto> pageList(@RequestBody @Validated RiskRevokeCreditRuleGroupSearchPageEntity entity) throws Exception {
//
//        if (!Strings.isNullOrEmpty(entity.getGroupCode())) {
//            entity.setGroupCode(entity.getGroupCode().replace("\\", "\\\\"));
//        }
        ResultWithPagerDto<RiskRevokeCreditRuleGroupDto> result =
                riskRevokeCreditRuleGroupService.searchPageList(entity);

        return result;
    }

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

        RiskRevokeCreditRuleGroupDto riskRevokeCreditRuleGroupDto =
                riskRevokeCreditRuleGroupService.searchSingle(entity);

        return BaseResultDto.success(riskRevokeCreditRuleGroupDto);
    }


    @PostMapping("/{method}/group/list")
    public BaseResultDto<List<String>> searchGroupList(@PathVariable("method") String method,
                                                       @RequestBody @Validated RiskRevokeCreditRuleGroupSearchGroupByPlatformEntity 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 RiskRevokeCreditRuleGroupSearchLoginByGroupEntity 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<RiskRevokeCreditRuleExcludeLoginSaveEntity> getRiskRevokeCreditRuleExcludeLoginSaveEntity(String platform, List<Long> excludeLogins) {
        if (excludeLogins == null || excludeLogins.size() <= 0) {
            return null;
        }
        List<RiskRevokeCreditRuleExcludeLoginSaveEntity> excludeLoginSaveEntities =
                new ArrayList<>(excludeLogins.size());
        for (Long login : excludeLogins) {
            RiskRevokeCreditRuleExcludeLoginSaveEntity riskRevokeCreditRuleExcludeLoginSaveEntity =
                    new RiskRevokeCreditRuleExcludeLoginSaveEntity();
            riskRevokeCreditRuleExcludeLoginSaveEntity.setPlatform(platform);
            riskRevokeCreditRuleExcludeLoginSaveEntity.setLogin(login);
            excludeLoginSaveEntities.add(riskRevokeCreditRuleExcludeLoginSaveEntity);
        }
        return excludeLoginSaveEntities;
    }
}
