package com.ssm.manage.service.impl;

import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.core.utils.StoreParaUtils;
import com.ssm.manage.dao.SsmRefundRecordDao;
import com.ssm.manage.dao.SsmRefundRoomCashDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.AchiCommEntity;
import com.ssm.manage.query.*;
import com.ssm.manage.response.RefundCashResponse;
import com.ssm.manage.response.RefundRoomCashResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
@Slf4j
public class SsmRefundRoomCashServiceImpl extends BaseServiceImpl<RefundRoomCash> implements SsmRefundRoomCashService {

    @Autowired
    private SsmRefundRoomCashDao dao;

    @Autowired
    private SsmMemberCsService ssmMemberCsService;
    @Autowired
    private SsmServiceOrdersService ssmServiceOrdersService;

    @Autowired
    private SsmUploadFilesService ssmUploadFilesService;

    @Override
    public SsmRefundRoomCashDao getDao() {
        return dao;
    }

    @Override
    public List<RefundRoomCashResponse> selectList(RefundRoomCashRequest refundCashReqest) {
        RefundRoomCash refundCash = new RefundRoomCash();
        BeanUtils.copyProperties(refundCashReqest, refundCash);
        List<RefundRoomCash> list = dao.select(refundCash);
        List<RefundRoomCashResponse> cashResponses = new ArrayList<>();
        for (RefundRoomCash cash : list) {
            RefundRoomCashResponse refundCashResponse = new RefundRoomCashResponse();
            BeanUtils.copyProperties(cash, refundCashResponse);
            //设置业务员
//            refundCashResponse = this.getStaffList(cash, refundCashResponse);
            //设置文件
            UploadFilesRequest uploadFilesRequest = new UploadFilesRequest();
            uploadFilesRequest.setDocId(refundCashReqest.getRefundOrderId());
            uploadFilesRequest.setDocNum(cash.getRefundNum());
            uploadFilesRequest.setDocType(Constant.doc_type_refund_cash);
            List<UploadFiles> filesList = ssmUploadFilesService.selectList(uploadFilesRequest);
            refundCashResponse.setFileLists(filesList);
            cashResponses.add(refundCashResponse);
        }
        return cashResponses;
    }



    @Override
    public int addRefundCashSalem(List<RefundRoomCashRequest> list, RefundOrderList refundOrderList, BaseStore baseStore1) throws SmsException {
        int i = 0;
        for (RefundRoomCashRequest refundCashReqest : list) {
            //校验收银退款与收银单支付金额大小
            if (BigDecimalUtils.compareTo(refundCashReqest.getCashRefundMoney(),refundCashReqest.getTransactionPrice()) == 1){
                throw new SmsException(ResponseCode.PRICE_COMPARE_MORE_THAN,"退款金额比成交金额");
            }
            RefundRoomCash refundCash = new RefundRoomCash();
            //参数赋值
            BeanUtils.copyProperties(refundCashReqest, refundCash);
            refundCash.setRefundNum(refundOrderList.getRefundNum());
            refundCash.setRefundDate(refundOrderList.getRefundDate());
            refundCash.setRoomCashId(refundCashReqest.getId());
            refundCash.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            refundCash.setStoreId(refundOrderList.getStoreId());

            try {
                i = dao.saveNotNull(refundCash);
            } catch (Exception e) {
                log.error("SsmRefundCashServiceImpl.addRefundCash:", e);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            }
        }
        return i;
    }

    @Override
    @Transactional
    public int updateRefundCash(List<RefundRoomCashRequest> list, RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        //先将原消费退明细记录进行删除
        RefundRoomCash cash = new RefundRoomCash();
        cash.setRefundNum(refundOrderList.getRefundNum());
        int j = dao.delete(cash);
        if (j > 0) {
            for (RefundRoomCashRequest refundCashReqest : list) {
                //校验收银退款与收银单支付金额大小
                if (BigDecimalUtils.compareTo(refundCashReqest.getCashRefundMoney(), refundCashReqest.getTransactionPrice()) == 1) {
                    throw new SmsException(ResponseCode.PRICE_COMPARE_MORE_THAN, "退款金额比成交金额");
                }
                RefundRoomCash refundCash = new RefundRoomCash();
                //参数赋值
                BeanUtils.copyProperties(refundCashReqest, refundCash);
                refundCash.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                refundCash.setRefundNum(refundOrderList.getRefundNum());
                refundCash.setRefundDate(refundOrderList.getRefundDate());
                if (null == refundCash.getId()) {
                    refundCash.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                }
                try {
                    i = dao.saveNotNull(refundCash);
                } catch (Exception e) {
                    log.error("SsmRefundCashServiceImpl.updateRefundCash:", e);
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
                }
            }
        }else {
            log.error("SsmRefundCashServiceImpl.updateRefundCash:","原消费退明细记录删除失败");
            throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
        }
        return i;
    }


    @Override
    @Transactional
    public int audit(RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        //查询消费退单据
        RefundRoomCash cash = new RefundRoomCash();
        cash.setStoreId(refundOrderList.getStoreId());
        cash.setRefundNum(refundOrderList.getRefundNum());
        List<RefundRoomCash> refundCashList = dao.select(cash);
        //查询消费退可以退的消费单最长天数
        StoreParament storeParament = StoreParaUtils.getStorePara(refundOrderList.getStoreId(), Constant.refund_day);
        if (!"0".equals(storeParament.getParamentValue())) {

            for (RefundRoomCash refundCash : refundCashList) {
                int days = (int) DateUtils.getDayDiff(refundCash.getDocDate(), new Date());
                if (Integer.parseInt(storeParament.getParamentValue()) < days){
                    throw new SmsException(ResponseCode.REFUND_NOT_PERMISSION,"超过允许退款的最长天数");
                }
                //消费退款单对应收银单作废
                i = ssmServiceOrdersService.cancelRowOnly(refundCash, refundOrderList);

                //将收银退款单据信息添加到评价表
                MemberCsRequest memberCsRequest = new MemberCsRequest();
                BeanUtils.copyProperties(refundCash, memberCsRequest);
                memberCsRequest.setDocId(refundOrderList.getId());
                memberCsRequest.setDocNum(refundOrderList.getRefundNum());
                memberCsRequest.setDocType(Constant.doc_type_refund_cash_room);
                memberCsRequest.setDocDate(refundOrderList.getRefundDate());
                memberCsRequest.setTradeId(refundCash.getProjectId());
                memberCsRequest.setTradeName(refundCash.getProjectName());
                memberCsRequest.setTradeMoney(refundCash.getTransactionPrice());
                StringBuffer staffIds = new StringBuffer();
                StringBuffer staffNames = new StringBuffer();
                ssmMemberCsService.addMemberCs(memberCsRequest);
            }
        }
        return i;
    }

    @Override
    @Transactional
    public int unAudit(RefundOrderList refundOrderList) throws SmsException {
        int i = 0;
        //查询消费退单据
        RefundRoomCash cash = new RefundRoomCash();
        cash.setStoreId(refundOrderList.getStoreId());
        cash.setRefundNum(refundOrderList.getRefundNum());
        List<RefundRoomCash> refundCashList = dao.select(cash);
        for (RefundRoomCash refundCash : refundCashList) {
            //消费退款单对应收银单恢复
            i = ssmServiceOrdersService.restoreRowOnly(refundCash, refundOrderList);
        }
        //将评价单进行删除操作
        MemberCs memberCs = new MemberCs();
        memberCs.setDocId(refundOrderList.getId());
        ssmMemberCsService.delete(memberCs);
        return i;
    }
}
