package com.cool.store.service.impl;


import com.cool.store.dao.EnterpriseUserDAO;
import com.cool.store.dao.PosAndOrderInfoDAO;
import com.cool.store.dao.ShopStageInfoDAO;
import com.cool.store.entity.PosAndOrderInfoDO;
import com.cool.store.enums.PosAndOrderEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
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 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 PosAndOrderInfoDAO posAndOrderInfoDAO;
    @Autowired
    private ShopStageInfoDAO shopStageInfoDAO;
    @Autowired
    private EnterpriseUserDAO enterpriseUserDAO;

    @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);
            }
            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);
        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()));
        }
        return response;
    }
}




