package com.crm.pay.controller;

import com.crm.pay.service.FinanceDepositService;
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.entity.base.SingleLongEntity;
import com.crm.rely.backend.model.entity.user.info.UserInfoEntity;
import com.crm.rely.backend.model.dto.finance.deposit.FinanceDepositDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.finance.deposit.FinanceDepositApproveEntity;
import com.crm.rely.backend.model.entity.finance.deposit.FinanceDepositDeleteEntity;
import com.crm.rely.backend.model.entity.finance.deposit.FinanceDepositSearchListEntity;
import com.crm.rely.backend.model.pojo.table.FinanceDepositTable;
import com.crm.rely.backend.service.RedisService;
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;


@RestController
@RequestMapping("/finance/deposit")
public class FinanceDepositController {

    @Autowired
    private FinanceDepositService financeDepositService;

    @Autowired
    private RedisService redisService;

    @PostMapping("/search/list")
    public ResultWithPagerDto<FinanceDepositDto> searchList(@RequestBody @Validated FinanceDepositSearchListEntity entity
            , CustomInfoEntity infoEntity) throws Exception {
        entity.setCustomId(infoEntity.getId());
        ResultWithPagerDto<FinanceDepositDto> resultWithPagerDto = financeDepositService.searchPageList(entity);
        return resultWithPagerDto;
    }

    @PostMapping("/manager/approve")
    @FeignClientAnnotation
    public BaseResultDto approve(@RequestBody @Validated FinanceDepositApproveEntity entity) throws Exception {
        if (!StatusConstants.PASSED_STATUS.equals(entity.getStatus()) && !StatusConstants.REJECTED_STATUS.equals(entity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }
        FinanceDepositTable financeDepositTable = financeDepositService.approve(entity);

        //更新缓存
        if (StatusConstants.PASSED_STATUS.equals(entity.getStatus())) {
            String key = PrefixEnum.PREFIX_CUSTOM.getCode() + financeDepositTable.getCustomId();
            CustomInfoEntity infoEntity =
                    redisService.getEntity(key,
                            CustomInfoEntity.class);

            //更新入金状态
            if (infoEntity != null) {
                if (infoEntity.getPayStatus() == null || !infoEntity.getPayStatus().equals(1)) {
                    infoEntity.setPayStatus(1);
                    redisService.save(key, infoEntity);
                }
            }
        }
        return BaseResultDto.success();
    }

    @PostMapping("/manager/delete")
    @FeignClientAnnotation
    public BaseResultDto delete(@RequestBody @Validated FinanceDepositDeleteEntity entity) throws Exception {
        financeDepositService.delete(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<FinanceDepositDto> searchList(@RequestBody @Validated FinanceDepositSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }
        ResultWithPagerDto<FinanceDepositDto> resultWithPagerDto = financeDepositService.searchPageList(entity);
        return resultWithPagerDto;
    }

    @PostMapping("/manager/search/single")
    @FeignClientAnnotation
    public BaseResultDto<FinanceDepositDto> searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        FinanceDepositDto financeDepositDto = financeDepositService.searchSingle(entity.getId());
        return BaseResultDto.success(financeDepositDto);
    }

}
