package com.cool.store.service.impl;

import ch.qos.logback.classic.jmx.MBeanUtil;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.InvoicingDAO;
import com.cool.store.dao.ShopStageInfoDAO;
import com.cool.store.dto.InvoicingDTO;
import com.cool.store.entity.InvoicingDO;
import com.cool.store.entity.ShopStageInfoDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.point.ShopSubStageEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.service.InvoicingService;
import com.cool.store.service.PreparationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * @Author suzhuhong
 * @Date 2024/12/31 13:53
 * @Version 1.0
 */
@Service
@Slf4j
public class InvoicingServiceImpl implements InvoicingService {

    @Resource
    private InvoicingDAO invoicingDAO;
    @Resource
    PreparationService preparationService;
    @Resource
    private ShopStageInfoDAO shopStageInfoDAO;

    @Override
    public Boolean addInvoicing(InvoicingDTO invoicingDTO, LoginUserInfo userInfo) {
        if (invoicingDTO==null||invoicingDTO.getId()!=null){
            throw  new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        InvoicingDO invoicing = invoicingDAO.getByShopId(invoicingDTO.getShopId());
        if (invoicing!=null){
            throw new ServiceException(ErrorCodeEnum.INVOICING_EXIST);
        }
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(invoicingDTO.getShopId(), ShopSubStageEnum.SHOP_STAGE_8_5);

        InvoicingDO invoicingDO = new InvoicingDO();
        BeanUtil.copyProperties(invoicingDTO, invoicingDO);
        invoicingDO.setCreateUserId(userInfo.getUserId());
        invoicingDAO.add(invoicingDO);

        //新数据待提交时初始化   老数据已数据处理 阶段状态已完成 只做插入操作 无需初始化
        if (shopSubStageInfo!=null&&ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_85_1.getShopSubStageStatus().equals(shopSubStageInfo.getShopSubStageStatus())){
            shopStageInfoDAO.updateShopStageInfo(invoicingDTO.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_85_3);
            preparationService.updateShopStatus(invoicingDTO.getShopId());
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean editInvoicing(InvoicingDTO invoicingDTO,LoginUserInfo userInfo) {
        if (invoicingDTO==null||invoicingDTO.getId()==null){
            throw  new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        InvoicingDO invoicing = invoicingDAO.getById(invoicingDTO.getId());
        if (invoicing==null){
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(invoicingDTO.getShopId(), ShopSubStageEnum.SHOP_STAGE_8_5);
        //已完成支持编辑
        if (shopSubStageInfo!=null&&!ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_85_3.getShopSubStageStatus().equals(shopSubStageInfo.getShopSubStageStatus())){
            throw new ServiceException(ErrorCodeEnum.STATUS_NOT_SUPPORT_SUMMIT);
        }
        InvoicingDO invoicingDO = new InvoicingDO();
        BeanUtil.copyProperties(invoicingDTO, invoicingDO);
        invoicingDO.setCreateTime(invoicing.getCreateTime());
        invoicingDO.setCreateUserId(invoicing.getCreateUserId());
        invoicingDO.setUpdateUserId(userInfo.getUserId());
        invoicingDAO.update(invoicingDO);
        return Boolean.TRUE;
    }

    @Override
    public InvoicingDTO getInvoicingByShopId(Long shopId) {
        if (shopId==null){
            throw  new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        InvoicingDO invoicingDO = invoicingDAO.getByShopId(shopId);
        InvoicingDTO invoicingDTO = new InvoicingDTO();
        BeanUtil.copyProperties(invoicingDO, invoicingDTO);
        log.info("invoicingDO:{}", JSONObject.toJSONString(invoicingDO));
        return invoicingDTO;
    }
}
