package com.crm.manager.controller;

import com.crm.manager.service.AccountInfoFeignService;
import com.crm.manager.service.CustomInfoService;
import com.crm.manager.service.DataRoleService;
import com.crm.manager.service.activity.AccountApplyFeignService;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.base.BaseReportResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.custom.CustomInfoDto;
import com.crm.rely.backend.core.dto.ib.account.AccountInfoDto;
import com.crm.rely.backend.core.dto.ib.account.AccountInfoSumDto;
import com.crm.rely.backend.core.entity.account.info.AccountFullInfoDto;
import com.crm.rely.backend.core.entity.account.info.AccountInfoSingleEntity;
import com.crm.rely.backend.core.entity.custom.info.IbInfoEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.ib.account.AccountInfoListEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by max on 2020/6/17.
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountInfoFeignService accountInfoFeignService;
    @Autowired
    private CustomInfoService customInfoService;
    @Autowired
    private DataRoleService dataRoleService;

    @Autowired
    private AccountApplyFeignService accountApplyFeignService;


    @PostMapping("/list")
    public BaseReportResultDto<List<AccountInfoDto>, AccountInfoSumDto> accountList(@RequestBody AccountInfoListEntity entity, InfoEntity infoEntity) throws Exception {

        IbInfoEntity ibInfoEntity = infoEntity.getIbInfo();

        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
            entity.setStamp(ibInfoEntity.getStamp());
        }
        entity.setGroupId(ibInfoEntity.getGroupId());

        return accountInfoFeignService.manageAccountList(entity);
    }


    /**
     * 查询单个交易账户信息
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/single")
    public BaseResultDto<AccountFullInfoDto> accountInfo(@RequestBody AccountInfoSingleEntity entity,
                                                         InfoEntity infoEntity) throws Exception {

        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
            dataRoleService.validateByLoginAndStamp(entity.getLogin(), infoEntity.getIbInfo().getStamp());
        }

        /*
            获取客户信息
         */
        CustomInfoDto customInfoDto = customInfoService.getSingleCustomByLogin(entity.getLogin());
        /*
            获取交易账户信息
         */
        BaseResultDto<AccountInfoDto> accountInfoDtoBaseResultDto = accountInfoFeignService.managerSearchSingle(entity);
        if (accountInfoDtoBaseResultDto.getCode() != 200) {
            BaseResultDto.error(accountInfoDtoBaseResultDto.getMsg());
        }
        AccountInfoDto accountInfoDto = accountInfoDtoBaseResultDto.getData();

        //返回数据
        AccountFullInfoDto accountFullInfoDto = new AccountFullInfoDto();
        accountFullInfoDto.setAccount(accountInfoDto);
        accountFullInfoDto.setCustom(customInfoDto);

        return BaseResultDto.success(accountFullInfoDto);
    }

}
