package com.crm.manager.controller;

import com.crm.manager.service.AccountInvestorPasswordChangeService;
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.deal.reset.AccountDealPasswordResetDto;
import com.crm.rely.backend.core.dto.account.investor.AccountInvestorPasswordChangeDto;
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.deal.reset.AccountDealPasswordResetSearchEntity;
import com.crm.rely.backend.core.entity.account.investor.AccountInvestorPasswordChangeApproveEntity;
import com.crm.rely.backend.core.entity.account.investor.AccountInvestorPasswordChangeApproveManagerEntity;
import com.crm.rely.backend.core.entity.account.investor.AccountInvestorPasswordChangeDeleteEntity;
import com.crm.rely.backend.core.entity.account.investor.AccountInvestorPasswordChangeSearchEntity;
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/investor/password/change")
public class AccountInvestorPasswordChangeController {

    @Autowired
    private AccountInvestorPasswordChangeService accountInvestorPasswordChangeService;

    @PostMapping("/approve")
    public BaseResultDto approve(@RequestBody @Validated AccountInvestorPasswordChangeApproveEntity entity) throws Exception {
        if (!Constants.PASSED_STATUS.equals(entity.getStatus()) && !Constants.REJECTED_STATUS.equals(entity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        entity.setInvestorStatus(Constants.MANAGER_PROCESSING_STATUS);
        entity.setOperationType(Constants.OPERATION_TYPE_MANUAL);
        accountInvestorPasswordChangeService.approve(entity);
        return BaseResultDto.success();
    }

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

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

    @PostMapping("/searcher/list")
    public BaseResultDto searcherList(@RequestBody @Validated AccountInvestorPasswordChangeSearchEntity 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);
        }
        if (entity.getStartDate() != null) {
            entity.setStartDate(DateUtil.parseDate(entity.getStartDate()));
        }
        if (entity.getEndDate() != null) {
            Date endDate = DateUtil.operationDay(entity.getEndDate(), 1);
            entity.setEndDate(DateUtil.parseDate(endDate));
        }
        ResultWithPagerDto<AccountInvestorPasswordChangeDto> resultWithPagerDto = accountInvestorPasswordChangeService.searchList(entity);
        return resultWithPagerDto;
    }
    @PostMapping("/custom/searcher/list")
    @FeignClientAnnotation
    public BaseResultDto searcherCustomList(@RequestBody @Validated AccountInvestorPasswordChangeSearchEntity entity) throws Exception {
        if (entity.getStartDate() != null) {
            entity.setStartDate(DateUtil.parseDate(entity.getStartDate()));
        }
        if (entity.getEndDate() != null) {
            Date endDate = DateUtil.operationDay(entity.getEndDate(), 1);
            entity.setEndDate(DateUtil.parseDate(endDate));
        }
        ResultWithPagerDto<AccountInvestorPasswordChangeDto> resultWithPagerDto = accountInvestorPasswordChangeService.searchList(entity);
        return resultWithPagerDto;
    }
    @PostMapping("/searcher/single")
    public BaseResultDto searcherSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        AccountInvestorPasswordChangeDto accountInvestorPasswordChangeDto = accountInvestorPasswordChangeService.searchSingle(entity.getId());
        return BaseResultDto.success(accountInvestorPasswordChangeDto);
    }

}
