package com.crm.manager.controller;

import com.crm.manager.service.AccountDealPasswordResetService;
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.change.AccountDealPasswordChangeDto;
import com.crm.rely.backend.core.dto.account.deal.reset.AccountDealPasswordResetDto;
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.change.AccountDealPasswordChangeSearchEntity;
import com.crm.rely.backend.core.entity.account.deal.reset.AccountDealPasswordResetApproveEntity;
import com.crm.rely.backend.core.entity.account.deal.reset.AccountDealPasswordResetApproveManagerEntity;
import com.crm.rely.backend.core.entity.account.deal.reset.AccountDealPasswordResetDeleteEntity;
import com.crm.rely.backend.core.entity.account.deal.reset.AccountDealPasswordResetSearchEntity;
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/deal/password/reset")
public class AccountDealPasswordResetController {

    @Autowired
    private AccountDealPasswordResetService accountDealPasswordResetService;

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

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

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

    @PostMapping("/searcher/list")
    public BaseResultDto searcherList(@RequestBody @Validated AccountDealPasswordResetSearchEntity 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<AccountDealPasswordResetDto> resultWithPagerDto = accountDealPasswordResetService.searchList(entity);
        return resultWithPagerDto;
    }

    @PostMapping("/custom/searcher/list")
    @FeignClientAnnotation
    public BaseResultDto searcherCustomList(@RequestBody @Validated AccountDealPasswordResetSearchEntity 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<AccountDealPasswordResetDto> resultWithPagerDto = accountDealPasswordResetService.searchList(entity);
        return resultWithPagerDto;
    }

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

}
