package com.crm.manager.controller;

import com.crm.manager.service.AccountDealPasswordChangeService;
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.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.entity.account.deal.change.AccountDealPasswordChangeApproveEntity;
import com.crm.rely.backend.core.entity.account.deal.change.AccountDealPasswordChangeApproveManagerEntity;
import com.crm.rely.backend.core.entity.account.deal.change.AccountDealPasswordChangeDeleteEntity;
import com.crm.rely.backend.core.entity.account.deal.change.AccountDealPasswordChangeSearchEntity;
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/change")
public class AccountDealPasswordChangeController {

    @Autowired
    private AccountDealPasswordChangeService accountDealPasswordChangeService;

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

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

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

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

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

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

}
