package com.crm.manager.controller;

import com.crm.manager.service.AccountLeverageChangeService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.account.leverage.AccountLeverageSearchListDto;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.entity.account.leverage.AccountLeverageChangeApproveEntity;
import com.crm.rely.backend.core.entity.account.leverage.AccountLeverageChangeDeleteEntity;
import com.crm.rely.backend.core.entity.account.leverage.AccountLeverageChangeSearchEntity;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.Date;

/**
 * @description: 账号杠杆申请管理
 * @author: houn
 * @create: 2020-07-07 18:01
 **/
@RestController
@RequestMapping("/account/leverage")
public class AccountLeverageChangeController {

    @Autowired
    private AccountLeverageChangeService accountLeverageChangeService;

    @PostMapping("/approve")
    public BaseResultDto approve(@RequestBody @Validated AccountLeverageChangeApproveEntity entity) throws Exception {
        if (!Constants.PASSED_STATUS.equals(entity.getStatus()) && !Constants.REJECTED_STATUS.equals(entity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        if (Constants.PASSED_STATUS.equals(entity.getStatus())) {
            entity.setLeverageStatus(Constants.MANAGER_COMPLETED_STATUS);
        } else {
            entity.setLeverageStatus(null);
        }
        entity.setOperationType(Constants.OPERATION_TYPE_MANUAL);
        accountLeverageChangeService.approve(entity);
        return BaseResultDto.success();
    }

//    @PostMapping("/approve/manager")
//    public BaseResultDto approveByManager(@RequestBody @Validated AccountLeverageChangeApproveManagerEntity entity) throws Exception {
//        if (!Constants.MANAGER_COMPLETED_STATUS.equals(entity.getLeverageStatus())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
//        accountLeverageChangeService.approve(entity);
//        return BaseResultDto.success();
//    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated AccountLeverageChangeDeleteEntity entity) throws Exception {
        accountLeverageChangeService.delete(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/searcher/list")
    public BaseResultDto searcherList(@RequestBody @Validated AccountLeverageChangeSearchEntity entity) throws Exception {

        ResultWithPagerDto<AccountLeverageSearchListDto> resultWithPagerDto = accountLeverageChangeService.searchList(entity);
        return resultWithPagerDto;
    }

    @PostMapping("/custom/searcher/list")
    @FeignClientAnnotation
    public BaseResultDto searcherCustomList(@RequestBody @Validated AccountLeverageChangeSearchEntity entity, InfoEntity infoEntity) throws Exception {
//        if (!PrefixEnum.PREFIX_SYSTEM.equals(infoEntity.getPrefix())) {
//            entity.setStamp(infoEntity.getIbInfo().getStamp());
//        }else {
//            entity.setStamp(null);
//        }
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        } else {
            entity.setStamp(null);
        }

        ResultWithPagerDto<AccountLeverageSearchListDto> resultWithPagerDto = accountLeverageChangeService.searchList(entity);
        return resultWithPagerDto;
    }

    @PostMapping("/searcher/single")
    public BaseResultDto searcherSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        AccountLeverageSearchListDto accountLeverageDto = accountLeverageChangeService.searchSingle(entity.getId());
        return BaseResultDto.success(accountLeverageDto);
    }

}
