package com.crm.manager.controller;

import com.crm.manager.service.activity.AccountApplyFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.account.apply.AccountApplyDto;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.entity.account.apply.AccountApplyApproveEntity;
import com.crm.rely.backend.core.entity.account.apply.AccountApplySearchListEntity;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
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;

/**
 * Created by max on 2020/7/14.
 */
@RestController
@RequestMapping("/account/apply")
public class AccountApplyController {

    @Autowired
    private AccountApplyFeignService accountApplyFeignService;

    @PostMapping("/approve")
    public BaseResultDto approve(@RequestBody @Validated AccountApplyApproveEntity entity) throws Exception {

        if (!Constants.PASSED_STATUS.equals(entity.getStatus()) && !Constants.REJECTED_STATUS.equals(entity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }

//        if (Constants.REJECTED_STATUS.equals(entity.getStatus())) {
//            entity.setAccountStatus(Constants.MANAGER_PENDING_STATUS);
//        } else {
//            entity.setAccountStatus(Constants.MANAGER_COMPLETED_STATUS);
//        }
//        if (Constants.PASSED_STATUS.equals(entity.getStatus()) && entity.getLogin() == null) {
//            return BaseResultDto.error(Constants.LOGIN_NOT_NULL_ERROR);
//        }
//        entity.setOperationType(Constants.OPERATION_TYPE_MANUAL);

        return accountApplyFeignService.approve(entity);
    }

    @PostMapping("/search/list")
    public BaseResultDto accountList(@RequestBody AccountApplySearchListEntity 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);
        }
//        entity.setStatus(1);
        return accountApplyFeignService.searchPageList(entity);
    }

    /**
     * 获取单个信息
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/search/single")
    public BaseResultDto managerSearchSingle(@RequestBody SingleLongEntity entity, InfoEntity infoEntity) throws Exception {

        AccountApplyDto accountApplyDto = accountApplyFeignService.single(entity);
        return BaseResultDto.success(accountApplyDto);
    }

}
