package com.cool.store.service.impl;


import com.cool.store.dao.*;
import com.cool.store.entity.LineInfoDO;
import com.cool.store.entity.PosAndOrderInfoDO;
import com.cool.store.entity.ShopInfoDO;
import com.cool.store.entity.ShopStageInfoDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.PosAndOrderEnum;
import com.cool.store.enums.point.ShopSubStageEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.PostAndOrderRequest;
import com.cool.store.response.PosAndOrderResponse;
import com.cool.store.service.PosAndOrderInfoService;
import com.cool.store.mapper.PosAndOrderInfoMapper;
import com.cool.store.service.PreparationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @author EDY
 * @description 针对表【xfsg_pos_and_order_info(pos/订货系统表)】的数据库操作Service实现
 * @createDate 2024-10-09 14:39:11
 */
@Service
public class PosAndOrderInfoServiceImpl implements PosAndOrderInfoService {
    @Resource
    private LineInfoDAO lineInfoDAO;
    @Resource
    private ShopInfoDAO shopInfoDAO;
    @Resource
    private PosAndOrderInfoDAO posAndOrderInfoDAO;
    @Autowired
    private ShopStageInfoDAO shopStageInfoDAO;
    @Autowired
    private EnterpriseUserDAO enterpriseUserDAO;
    @Autowired
    private PreparationService preparationService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer submitOrUpdate(PostAndOrderRequest request, String user) {
        PosAndOrderInfoDO posAndOrderInfoDO = posAndOrderInfoDAO.selectOneByShopId(request.getShopId(), request.getType());
        PosAndOrderInfoDO posAndOrderInfo = request.toDO();
        posAndOrderInfo.setCreateUser(user);
        posAndOrderInfo.setCreateTime(new Date());
        if (Objects.isNull(posAndOrderInfoDO)) {
            if (request.getType().equals(PosAndOrderEnum.POS.getCode())) {
                shopStageInfoDAO.updateShopStageInfo(request.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_163);
            } else if (request.getType().equals(PosAndOrderEnum.ORDER.getCode())) {
                shopStageInfoDAO.updateShopStageInfo(request.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_173);
            } else if (request.getType().equals(PosAndOrderEnum.XIN_FA_SYS.getCode())) {
                shopStageInfoDAO.updateShopStageInfo(request.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_235);
            } else if (request.getType().equals(PosAndOrderEnum.TENT_PASS.getCode())) {
                shopStageInfoDAO.updateShopStageInfo(request.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_245);
            }
            preparationService.updateShopStatus(request.getShopId());
            return posAndOrderInfoDAO.insertSelective(posAndOrderInfo);
        } else {
            return posAndOrderInfoDAO.updateByShopIdSelective(posAndOrderInfo);
        }
    }

    @Override
    public PosAndOrderResponse get(Long shopId, Integer type) {
        PosAndOrderResponse response = new PosAndOrderResponse();
        PosAndOrderInfoDO posAndOrderInfoDO = posAndOrderInfoDAO.selectOneByShopId(shopId, type);
        ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(shopId);
        LineInfoDO lineInfoDO = lineInfoDAO.getLineInfo(shopInfo.getLineId());
        if (Objects.nonNull(posAndOrderInfoDO)) {
            response.setShopId(posAndOrderInfoDO.getShopId());
            response.setAccount(posAndOrderInfoDO.getAccount());
            response.setType(posAndOrderInfoDO.getType());
            response.setPassword(posAndOrderInfoDO.getPassword());
            response.setRemark(posAndOrderInfoDO.getRemark());
            response.setCreateTime(posAndOrderInfoDO.getCreateTime());
            response.setCreateUser(enterpriseUserDAO.getUserName(posAndOrderInfoDO.getCreateUser()));
        }
        else{
            if (type.equals(PosAndOrderEnum.POS.getCode())){
                response.setAccount(lineInfoDO.getMobile());
            }
            if (type.equals(PosAndOrderEnum.XIN_FA_SYS.getCode())){
                response.setAccount(shopInfo.getShopCode());
            }

        }
        return response;
    }

    @Override
    public Boolean skip(Long shopId, Integer type) {
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopId, ShopSubStageEnum.SHOP_STAGE_24);
        if (!ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_240.getShopSubStageStatus().equals(shopSubStageInfo.getShopSubStageStatus())) {
            throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
        }
        shopStageInfoDAO.updateShopStageInfo(shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_244);
        preparationService.updateShopStatus(shopId);
        return Boolean.TRUE;
    }
}




