package com.crm.manager.controller;

import com.crm.manager.service.custom.CustomLoginFeignService;
import com.crm.manager.service.risk.RiskCloseTradeLoginFeignService;
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.login.RiskCloseTradeLoginDto;
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.close.trade.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/close/trade/login")
public class RiskCloseTradeLoginController {

    @Autowired
    private RiskCloseTradeLoginFeignService riskCloseTradeLoginFeignService;

    @Autowired
    private CustomLoginFeignService customLoginFeignService;

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

        return riskCloseTradeLoginFeignService.add(entity);
    }

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

        return riskCloseTradeLoginFeignService.update(entity);
    }

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

        return riskCloseTradeLoginFeignService.delete(entity);
    }

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

        return riskCloseTradeLoginFeignService.pageList(entity);
    }

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

        return riskCloseTradeLoginFeignService.single(entity);
    }

    @PostMapping("/{method}/login/list")
    public BaseResultDto<List<Long>> searchLoginList(@PathVariable("method") String method,
                                                     @RequestBody @Validated RiskCloseTradeSearchLoginByPlatformEntity 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);

    }
}
