package com.crm.custom.controller;

import com.crm.custom.service.*;
import com.crm.login.rely.backend.controller.BaseLoginController;
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.constant.StatusConstants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.exception.ServiceException;
import com.crm.rely.backend.model.constant.CustomConstants;
import com.crm.rely.backend.model.dto.custom.login.*;
import com.crm.rely.backend.model.entity.account.apply.AccountApplyAddEntity;
import com.crm.rely.backend.model.entity.account.apply.demo.AccountApplyDemoAddEntity;
import com.crm.rely.backend.model.entity.account.apply.firm.offer.AccountApplyFirmOfferAddEntity;
import com.crm.rely.backend.model.entity.custom.CustomLoginUpdateAssessmentByLoginEntity;
import com.crm.rely.backend.model.entity.custom.CustomLoginUpdateCheckByLoginEntity;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.custom.login.CustomLoginApproveEntity;
import com.crm.rely.backend.model.entity.custom.login.CustomLoginCheckByLoginEntity;
import com.crm.rely.backend.model.entity.custom.login.CustomLoginSearchListEntity;
import com.crm.rely.backend.model.entity.custom.login.asset.management.CustomLoginAssetManagementApproveEntity;
import com.crm.rely.backend.model.entity.custom.login.asset.management.CustomLoginAssetManagementSearchListEntity;
import com.crm.rely.backend.model.entity.custom.login.asset.management.CustomLoginAssetManagementUpdateCheckByLoginEntity;
import com.crm.rely.backend.model.entity.custom.login.demo.CustomLoginDemoApproveEntity;
import com.crm.rely.backend.model.entity.custom.login.demo.CustomLoginDemoSearchListEntity;
import com.crm.rely.backend.model.entity.custom.login.demo.CustomLoginDemoUpdateCheckByLoginEntity;
import com.crm.rely.backend.model.entity.custom.login.firm.offer.CustomLoginFirmOfferApproveEntity;
import com.crm.rely.backend.model.entity.custom.login.firm.offer.CustomLoginFirmOfferSearchListEntity;
import com.crm.rely.backend.model.entity.custom.login.firm.offer.CustomLoginFirmOfferUpdateCheckByLoginEntity;
import com.crm.rely.backend.model.entity.user.info.UserInfoEntity;
import com.crm.rely.backend.model.pojo.table.CustomLoginAssetManagementTable;
import com.crm.rely.backend.model.pojo.table.CustomLoginDemoTable;
import com.crm.rely.backend.model.pojo.table.CustomLoginFirmOfferTable;
import com.crm.rely.backend.model.pojo.table.CustomLoginTable;
import com.google.common.base.Strings;
import lombok.extern.log4j.Log4j;
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.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Log4j
@RestController
@RequestMapping("/custom/login")
public class CustomLoginController extends BaseLoginController {
    @Autowired
    private AccountApplyDemoService accountApplyDemoService;
    @Autowired
    private AccountApplyFirmOfferService accountApplyFirmOfferService;
    @Autowired
    private AccountApplyAssetManagementService accountApplyAssetManagementService;
    @Autowired
    private CustomLoginDemoService customLoginDemoService;
    @Autowired
    private CustomLoginFirmOfferService customLoginFirmOfferService;
    @Autowired
    private CustomLoginAssetManagementService customLoginAssetManagementService;
    @Autowired
    private WebVideoFeignService webVideoFeignService;

    @Autowired
    private AccountApplyService accountApplyService;
    @Autowired
    private CustomLoginService customLoginService;

    @Autowired
    private CustomInfoService customInfoService;

    @PostMapping("/demo/apply")
    public BaseResultDto<Object> addDemo(CustomInfoEntity infoEntity) throws Exception {
//        if (!CustomConstants.POSITION1.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION1);

//        BaseResultDto<Boolean> resultDto = webVideoFeignService.customValidateRead();
//        if (resultDto == null || resultDto.getCode() != 200 || !resultDto.getData()) {
//
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        AccountApplyDemoAddEntity entity = new AccountApplyDemoAddEntity();
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        entity.setAgentId(infoEntity.getAgentId());
        accountApplyDemoService.addAccountApplyDemo(entity);
        infoEntity.setPosition(CustomConstants.POSITION2);
        super.saveLoginInfo(infoEntity);
        return BaseResultDto.success();
    }

    @PostMapping("/demo/search/list")
    public ResultWithPagerDto<CustomLoginDemoDto> searchPageList(@RequestBody @Validated CustomLoginDemoSearchListEntity entity, CustomInfoEntity infoEntity) throws Exception {
        entity.setCId(infoEntity.getCId());

        validSortName(entity.getSortName());
        ResultWithPagerDto<CustomLoginDemoDto> result = customLoginDemoService.searchPageList(entity);

        return result;
    }

    @PostMapping("/demo/apply/check")
    public BaseResultDto<Object> applyCheck(@RequestBody @Validated CustomLoginDemoUpdateCheckByLoginEntity entity,
                                            CustomInfoEntity infoEntity) throws Exception {
//        if (!CustomConstants.POSITION2.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION2);
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        entity.setStatus(StatusConstants.APPLY_STATUS);
        customLoginDemoService.applyCheck(entity);
        return BaseResultDto.success();
    }

    /**
     * 放弃考核
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/demo/abandon")
    public BaseResultDto<Object> applyAbandon(@RequestBody @Validated CustomLoginDemoUpdateCheckByLoginEntity entity,
                                              CustomInfoEntity infoEntity) throws Exception {
//        if (!CustomConstants.POSITION2.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION2);
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        entity.setStatus(StatusConstants.APPLY_ABANDON_STATUS);
        customLoginDemoService.applyCheck(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/demo/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<CustomLoginDemoDto> managerSearchPageList(@RequestBody @Validated CustomLoginDemoSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }

        validSortName(entity.getSortName());
        ResultWithPagerDto<CustomLoginDemoDto> result = customLoginDemoService.searchPageList(entity);

        return result;
    }

    @PostMapping("/manager/demo/approve")
    @FeignClientAnnotation
    public BaseResultDto approveDemo(@RequestBody @Validated CustomLoginDemoApproveEntity entity) throws Exception {

        CustomLoginDemoTable table = customLoginDemoService.approve(entity);

        return BaseResultDto.success();
    }


    @PostMapping("/demo/condition")
    public BaseResultDto<CustomLoginConditionDto> conditionDemo(@RequestBody @Validated CustomLoginCheckByLoginEntity entity, CustomInfoEntity infoEntity) throws Exception {

//        if (!CustomConstants.POSITION2.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION2);
        entity.setCustomId(infoEntity.getId());
        CustomLoginConditionDto customLoginDemoConditionDto =
                customLoginDemoService.conditionDemo(entity);

        return BaseResultDto.success(customLoginDemoConditionDto);
    }

    @PostMapping("/demo/again/study")
    public BaseResultDto<Object> againStudy(@RequestBody @Validated CustomLoginCheckByLoginEntity entity,
                                            CustomInfoEntity infoEntity) throws Exception {

//        if (!CustomConstants.POSITION2.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }

        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION2);
        entity.setCustomId(infoEntity.getId());
        customLoginDemoService.studyDemo(entity);
        infoEntity.setPosition(CustomConstants.POSITION1);
        super.saveLoginInfo(infoEntity);
        return BaseResultDto.success();
    }


    @PostMapping("/firm/offer/apply")
    public BaseResultDto<Object> addFirmOffer(CustomInfoEntity infoEntity) throws Exception {
//        if (!CustomConstants.POSITION2.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }

        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION2);
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }

        /**
         * todo 核验客户状态 核验模拟账户状态
         */

        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())
                || !customLoginDemoService.existDemoStatus(infoEntity.getId(), StatusConstants.PASSED_STATUS)) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }

        AccountApplyFirmOfferAddEntity entity = new AccountApplyFirmOfferAddEntity();
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        entity.setAgentId(infoEntity.getAgentId());
        accountApplyFirmOfferService.addAccountApply(entity);
        infoEntity.setPosition(CustomConstants.POSITION3);
        super.saveLoginInfo(infoEntity);
        return BaseResultDto.success();
    }

    @PostMapping("/firm/offer/search/list")
    public ResultWithPagerDto<CustomLoginFirmOfferDto> searchFirmOfferPageList(@RequestBody @Validated CustomLoginFirmOfferSearchListEntity entity, CustomInfoEntity infoEntity) throws Exception {

        entity.setCId(infoEntity.getCId());

        validSortName(entity.getSortName());
        ResultWithPagerDto<CustomLoginFirmOfferDto> result = customLoginFirmOfferService.searchPageList(entity);

        return result;
    }

    @PostMapping("/firm/offer/apply/check")
    public BaseResultDto<Object> applyCheckFirmOffer(@RequestBody @Validated CustomLoginFirmOfferUpdateCheckByLoginEntity entity,
                                                     CustomInfoEntity infoEntity) throws Exception {
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
//        if (!CustomConstants.POSITION3.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION3);
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        entity.setStatus(StatusConstants.APPLY_STATUS);
        customLoginFirmOfferService.applyCheck(entity);
        return BaseResultDto.success();
    }

    /**
     * 放弃考核
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/firm/offer/abandon")
    public BaseResultDto<Object> applyAbandonFirmOffer(@RequestBody @Validated CustomLoginFirmOfferUpdateCheckByLoginEntity entity,
                                                       CustomInfoEntity infoEntity) throws Exception {
//        if (!CustomConstants.POSITION3.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION3);
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        entity.setStatus(StatusConstants.APPLY_ABANDON_STATUS);
        customLoginFirmOfferService.applyCheck(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/firm/offer/condition")
    public BaseResultDto<CustomLoginConditionDto> conditionFirmOffer(@RequestBody @Validated CustomLoginCheckByLoginEntity entity, CustomInfoEntity infoEntity) throws Exception {

//        if (!CustomConstants.POSITION3.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION3);
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        CustomLoginConditionDto customLoginDemoConditionDto =
                customLoginFirmOfferService.conditionFirmOffer(entity);

        return BaseResultDto.success(customLoginDemoConditionDto);
    }

    /**
     * 重新学习 验证条件
     *
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/firm/offer/again/study")
    public BaseResultDto<Object> againStudyFirmOffer(@RequestBody @Validated CustomLoginCheckByLoginEntity entity,
                                                     CustomInfoEntity infoEntity) throws Exception {

//        if (!CustomConstants.POSITION3.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION3);
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        customLoginFirmOfferService.studyFirmOffer(entity);

        infoEntity.setPosition(CustomConstants.POSITION1);
        super.saveLoginInfo(infoEntity);
        return BaseResultDto.success();
    }


    @PostMapping("/manager/firm/offer/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<CustomLoginFirmOfferDto> managerSearchFirmOfferPageList(@RequestBody @Validated CustomLoginFirmOfferSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }

        validSortName(entity.getSortName());
        ResultWithPagerDto<CustomLoginFirmOfferDto> result = customLoginFirmOfferService.searchPageList(entity);

        return result;
    }

    @PostMapping("/manager/firm/offer/approve")
    @FeignClientAnnotation
    public BaseResultDto approveFirmOffer(@RequestBody @Validated CustomLoginFirmOfferApproveEntity entity) throws Exception {

        if (StatusConstants.PASSED_STATUS.equals(entity.getStatus())) {
            if (entity.getBalance() == null || entity.getBalance().compareTo(BigDecimal.ZERO) <= 0) {
                return BaseResultDto.error(Constants.PARAMETER_ERROR);
            }
        } else {
            entity.setBalance(null);
        }
        CustomLoginFirmOfferTable table = customLoginFirmOfferService.approve(entity);

        if (StatusConstants.PASSED_STATUS.equals(entity.getStatus())) {
            CustomInfoEntity infoEntity = super.loginInfo(PrefixEnum.PREFIX_CUSTOM, table.getCustomId(),
                    CustomInfoEntity.class);
            if (infoEntity != null) {

                infoEntity.setPosition(CustomConstants.POSITION4);
            }
            super.saveLoginInfo(infoEntity);
        }

        return BaseResultDto.success();
    }


    @PostMapping("/asset/management/search/list")
    public ResultWithPagerDto<CustomLoginAssetManagementDto> searchAssetManagementPageList(@RequestBody @Validated CustomLoginAssetManagementSearchListEntity entity, CustomInfoEntity infoEntity) throws Exception {
        entity.setCId(infoEntity.getCId());
        entity.setCustomId(infoEntity.getId());
        entity.setReal(0);
        validSortName(entity.getSortName());
        ResultWithPagerDto<CustomLoginAssetManagementDto> result =
                customLoginAssetManagementService.searchPageList(entity);

        return result;
    }

    @PostMapping("/asset/management/real/search/list")
    public ResultWithPagerDto<CustomLoginAssetManagementDto> searchRealAssetManagementPageList(@RequestBody @Validated CustomLoginAssetManagementSearchListEntity entity, CustomInfoEntity infoEntity) throws Exception {
        entity.setCId(infoEntity.getCId());
        entity.setCustomId(infoEntity.getId());
        entity.setReal(1);
        validSortName(entity.getSortName());
        ResultWithPagerDto<CustomLoginAssetManagementDto> result =
                customLoginAssetManagementService.searchPageList(entity);

        return result;
    }

    @PostMapping("/asset/management/apply/check")
    public BaseResultDto<Object> applyCheckAssetManagement(@RequestBody @Validated CustomLoginAssetManagementUpdateCheckByLoginEntity entity,
                                                           CustomInfoEntity infoEntity) throws Exception {
//        if (!CustomConstants.POSITION4.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION4);
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        entity.setStatus(StatusConstants.APPLY_STATUS);
        customLoginAssetManagementService.applyCheck(entity);
        return BaseResultDto.success();
    }

    /**
     * 放弃考核
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/asset/management/abandon")
    public BaseResultDto<Object> applyAbandonAssetManagement(@RequestBody @Validated CustomLoginAssetManagementUpdateCheckByLoginEntity entity,
                                                             CustomInfoEntity infoEntity) throws Exception {
//        if (!CustomConstants.POSITION4.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION4);
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        entity.setStatus(StatusConstants.APPLY_ABANDON_STATUS);
        customLoginAssetManagementService.applyCheck(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/asset/management/condition")
    public BaseResultDto<CustomLoginConditionDto> conditionAssetManagement(@RequestBody @Validated CustomLoginCheckByLoginEntity entity, CustomInfoEntity infoEntity) throws Exception {

//        if (!CustomConstants.POSITION4.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION4);
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        CustomLoginConditionDto customLoginDemoConditionDto =
                customLoginAssetManagementService.conditionAssetManagement(entity);

        return BaseResultDto.success(customLoginDemoConditionDto);
    }

    /**
     * 重新学习 验证条件
     *
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/asset/management/again/study")
    public BaseResultDto<Object> againStudyAssetManagement(@RequestBody @Validated CustomLoginCheckByLoginEntity entity, CustomInfoEntity infoEntity) throws Exception {

//        if (!CustomConstants.POSITION4.equals(infoEntity.getPosition())) {
//            return BaseResultDto.error(Constants.NOT_PERMIT);
//        }
        customInfoService.validPosition(infoEntity.getId(), CustomConstants.POSITION4);
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        customLoginAssetManagementService.studyAssetManagement(entity);

        infoEntity.setPosition(CustomConstants.POSITION1);
        super.saveLoginInfo(infoEntity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/asset/management/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<CustomLoginAssetManagementDto> managerSearchAssetManagementPageList(@RequestBody @Validated CustomLoginAssetManagementSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }

        validSortName(entity.getSortName());
        ResultWithPagerDto<CustomLoginAssetManagementDto> result =
                customLoginAssetManagementService.searchPageList(entity);

        return result;
    }

    @PostMapping("/manager/asset/management/approve")
    @FeignClientAnnotation
    public BaseResultDto approveAssetManagement(@RequestBody @Validated CustomLoginAssetManagementApproveEntity entity) throws Exception {

        if (StatusConstants.PASSED_STATUS.equals(entity.getStatus())) {
            if (entity.getAmount() == null && entity.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
                return BaseResultDto.error(Constants.PARAMETER_ERROR);
            }
        } else {
            entity.setAmount(null);
        }
        CustomLoginAssetManagementTable table = customLoginAssetManagementService.approve(entity);


        return BaseResultDto.success();
    }


    @PostMapping("/apply")
    public BaseResultDto<Object> add(@RequestBody @Validated AccountApplyAddEntity entity,
                                     CustomInfoEntity infoEntity) throws Exception {

        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        entity.setAgentId(infoEntity.getAgentId());
        accountApplyService.addAccountApply(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto<CustomLoginDto> searchPageList(@RequestBody @Validated CustomLoginSearchListEntity entity, CustomInfoEntity infoEntity) throws Exception {
        entity.setCId(infoEntity.getCId());
        entity.setCustomId(infoEntity.getId());

        validSortName(entity.getSortName());
        ResultWithPagerDto<CustomLoginDto> result = customLoginService.searchPageList(entity);

        return result;
    }

    @PostMapping("/manager/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<CustomLoginDto> managerSearchPageList(@RequestBody @Validated CustomLoginSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }

        validSortName(entity.getSortName());
        ResultWithPagerDto<CustomLoginDto> result = customLoginService.searchPageList(entity);

        return result;
    }

    @PostMapping("/apply/assessment")
    public BaseResultDto<Object> applyAssessment(@RequestBody @Validated CustomLoginUpdateAssessmentByLoginEntity entity,
                                                 CustomInfoEntity infoEntity) throws Exception {
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        customLoginService.assessment(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/abandon/assessment")
    public BaseResultDto<Object> abandonAssessment(@RequestBody @Validated CustomLoginUpdateAssessmentByLoginEntity entity,
                                                   CustomInfoEntity infoEntity) throws Exception {
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        customLoginService.abandonAssessment(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/apply/check")
    public BaseResultDto<Object> applyCheck(@RequestBody @Validated CustomLoginUpdateCheckByLoginEntity entity,
                                            CustomInfoEntity infoEntity) throws Exception {
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        entity.setStatus(StatusConstants.APPLY_STATUS);
        customLoginService.applyCheck(entity);
        return BaseResultDto.success();
    }

    /**
     * 放弃考核
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/abandon")
    public BaseResultDto<Object> applyAbandonAssetManagement(@RequestBody @Validated CustomLoginUpdateCheckByLoginEntity entity,
                                                             CustomInfoEntity infoEntity) throws Exception {
        if (!StatusConstants.PASSED_STATUS.equals(infoEntity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setCustomId(infoEntity.getId());
        entity.setCId(infoEntity.getCId());
        entity.setStatus(StatusConstants.APPLY_ABANDON_STATUS);
        customLoginService.applyCheck(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/approve")
    @FeignClientAnnotation
    public BaseResultDto approve(@RequestBody @Validated CustomLoginApproveEntity entity) throws Exception {

        if (StatusConstants.PASSED_STATUS.equals(entity.getStatus())) {
            if (entity.getBalance() == null && entity.getBalance().compareTo(BigDecimal.ZERO) <= 0) {
                return BaseResultDto.error(Constants.PARAMETER_ERROR);
            }
        } else {
            entity.setBalance(null);
        }
        CustomLoginTable table = customLoginService.approve(entity);


        return BaseResultDto.success();
    }

    private void validSortName(String sortName) {
        if (!Strings.isNullOrEmpty(sortName)) {
            List<String> tableFields = new ArrayList<>(10);
            tableFields.add("last_ip");
            tableFields.add("add_ip");
            tableFields.add("modify_ip");
            if (!tableFields.contains(sortName)) {
                throw ServiceException.exception(Constants.FAIL);
            }
        }
    }

}
