package com.crm.manager.controller;

import com.crm.manager.service.CustomGroupService;
import com.crm.manager.service.risk.RiskGroupLeverageFeignService;
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.group.leverage.RiskGroupLeverageDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.risk.group.leverage.*;
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/group/leverage")
public class RiskGroupLeverageController {

    @Autowired
    private RiskGroupLeverageFeignService feignService;

    @Autowired
    private CustomGroupService customGroupService;

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

        return feignService.add(entity);
    }

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

        return feignService.update(entity);
    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated RiskGroupLeverageDeleteEntity entity) throws Exception {

        return feignService.delete(entity);
    }

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

        return feignService.pageList(entity);
    }

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

        return feignService.single(entity);
    }

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