package com.crm.manager.controller;

import com.alibaba.fastjson.JSON;
import com.crm.manager.service.SalesSalaryRecordService;
import com.crm.manager.service.UserInfoService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.sales.SalesBalanceDto;
import com.crm.rely.backend.core.dto.sales.SalesSalaryRecordDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.finance.balance.FinanceBalanceAddEntity;
import com.crm.rely.backend.core.entity.sales.SalesBalanceWithdrawalEntity;
import com.crm.rely.backend.core.entity.sales.SalesSalaryRecordAddEntity;
import com.crm.rely.backend.core.entity.sales.SalesSalaryRecordListEntity;
import com.crm.rely.backend.core.pojo.table.FinanceBalanceTable;
import com.crm.rely.backend.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.transaction.annotation.Transactional;
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.List;

@RestController
@RequestMapping("/sales/salary")
public class SalesSalaryRecordController {

    @Autowired
    private SalesSalaryRecordService salesSalaryRecordService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 销售获取薪资余额
     *
     * @return
     * @throws ServiceException
     */
    @PostMapping("/info")
    public BaseResultDto<SalesBalanceDto> salaryInfo(InfoEntity infoEntity) throws ServiceException {
        return BaseResultDto.success(userInfoService.getSalesBalanceById(infoEntity.getIbInfo().getId()));
    }


    @PostMapping("/withdrawal")
    public BaseResultDto<Object> salaryWithdrawal(@RequestBody @Validated SalesBalanceWithdrawalEntity entity, InfoEntity infoEntity) throws ServiceException {
        if (!PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            return BaseResultDto.error("Not permit");
        }
        entity.setSalesId(infoEntity.getIbInfo().getId());
        salesSalaryRecordService.salesBalanceWithdrawal(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/record/add")
    public BaseResultDto<Object> add(@RequestBody @Validated SalesSalaryRecordAddEntity entity) throws ServiceException {
        salesSalaryRecordService.save(entity);
        return BaseResultDto.success();
    }


    @PostMapping("/record/search/single")
    public BaseResultDto<Object> searchSingle(@RequestBody SingleLongEntity entity) throws ServiceException {
        return BaseResultDto.success(salesSalaryRecordService.searchById(entity.getId()));
    }


    @PostMapping("/record/search/list")
    public BaseResultWithPagerDto<List<SalesSalaryRecordDto>> searchList(@RequestBody SalesSalaryRecordListEntity entity) throws ServiceException {
        return salesSalaryRecordService.searchList(entity);
    }

//    @PostMapping("/callback")
//    public BaseResultDto<Object> salaryWithdrawalCallback( @RequestBody  FinanceBalanceTable financeBalanceTable) throws Exception {
//        salesSalaryRecordService.salesBalanceCallback(financeBalanceTable);
//        return BaseResultDto.success();
//    }

    @JmsListener(destination = Constants.SALES_BALANCE_CALLBACK)
    public void salaryWithdrawalCallback(String content) throws Exception {
        FinanceBalanceTable financeBalanceTable = JSON.parseObject(content, FinanceBalanceTable.class);
        salesSalaryRecordService.salesBalanceCallback(financeBalanceTable);
    }

}
