package com.crm.manager.controller;

import com.crm.manager.service.CustomGroupService;
import com.crm.manager.service.custom.CustomLoginFeignService;
import com.crm.manager.service.risk.RiskCloseTradeGroupFeignService;
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.close.trade.group.RiskCloseTradeGroupDto;
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.close.trade.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.List;

/**
 * 强制平仓规则 组别设置
 *
 * @author houn
 */
@RestController
@RequestMapping("/risk/close/trade/group")
public class RiskCloseTradeGroupController {

    @Autowired
    private RiskCloseTradeGroupFeignService riskCloseTradeGroupFeignService;

    @Autowired
    private CustomGroupService customGroupService;

    @Autowired
    private CustomLoginFeignService customLoginFeignService;

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

        return riskCloseTradeGroupFeignService.add(entity);
    }

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

        return riskCloseTradeGroupFeignService.update(entity);
    }

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

        return riskCloseTradeGroupFeignService.delete(entity);
    }

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

        return riskCloseTradeGroupFeignService.pageList(entity);
    }

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

        return riskCloseTradeGroupFeignService.single(entity);
    }


    @PostMapping("/{method}/group/list")
    public BaseResultDto<List<String>> searchGroupList(@PathVariable("method") String method,
                                                       @RequestBody @Validated RiskCloseTradeGroupSearchGroupByPlatformEntity 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 RiskCloseTradeGroupSearchLoginByGroupEntity 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);

    }

}
