package com.yiqi.order.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yiqi.common.core.domain.AuthHolder;
import com.yiqi.common.enums.ClothOrderFlowStatus;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.common.utils.StringUtils;
import com.yiqi.order.domain.OrderClothItem;
import com.yiqi.order.domain.dto.OrderClothItemOutFactoryDTO;
import com.yiqi.order.domain.dto.OrderClothItemOutFactoryQueryDTO;
import com.yiqi.order.service.IOrderClothItemFlowRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.order.mapper.OrderClothAdjunctMapper;
import com.yiqi.order.domain.OrderClothAdjunct;
import com.yiqi.order.service.IOrderClothAdjunctService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 订单衣服附件Service业务层处理
 *
 * @author xiaohei
 * @date 2023-10-06
 */
@Service
public class OrderClothAdjunctServiceImpl extends ServiceImpl<OrderClothAdjunctMapper, OrderClothAdjunct> implements IOrderClothAdjunctService {

    @Autowired
    private IOrderClothItemFlowRecordService orderClothItemFlowRecordService;

    /**
     * 查询订单衣服附件
     *
     * @param id 订单衣服附件主键
     * @return 订单衣服附件
     */
    @Override
    public OrderClothAdjunct selectOrderClothAdjunctById(Long id) {
        return baseMapper.selectOrderClothAdjunctById(id);
    }

    /**
     * 查询订单衣服附件列表
     *
     * @param orderClothAdjunct 订单衣服附件
     * @return 订单衣服附件
     */
    @Override
    public List<OrderClothAdjunct> selectOrderClothAdjunctList(OrderClothAdjunct orderClothAdjunct) {
        return baseMapper.selectOrderClothAdjunctList(orderClothAdjunct);
    }

    /**
     * 新增订单衣服附件
     *
     * @param orderClothAdjunct 订单衣服附件
     * @return 结果
     */
    @Override
    public int insertOrderClothAdjunct(OrderClothAdjunct orderClothAdjunct) {
        orderClothAdjunct.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertOrderClothAdjunct(orderClothAdjunct);
    }

    /**
     * 修改订单衣服附件
     *
     * @param orderClothAdjunct 订单衣服附件
     * @return 结果
     */
    @Override
    public int updateOrderClothAdjunct(OrderClothAdjunct orderClothAdjunct) {
        orderClothAdjunct.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateOrderClothAdjunct(orderClothAdjunct);
    }

    /**
     * 批量删除订单衣服附件
     *
     * @param ids 需要删除的订单衣服附件主键
     * @return 结果
     */
    @Override
    public int deleteOrderClothAdjunctByIds(Long[] ids) {
        return baseMapper.deleteOrderClothAdjunctByIds(ids);
    }

    /**
     * 删除订单衣服附件信息
     *
     * @param id 订单衣服附件主键
     * @return 结果
     */
    @Override
    public int deleteOrderClothAdjunctById(Long id) {
        return baseMapper.deleteOrderClothAdjunctById(id);
    }

    @Override
    public OrderClothAdjunct selectOrderClothAdjunctByAdjunctNo(String adjunctNo) {
        return baseMapper.selectOrderClothAdjunctByAdjunctNo(adjunctNo);
    }

    @Override
    public List<OrderClothAdjunct> queryWaitFactoryOrderClothAdjunctList(OrderClothItemOutFactoryQueryDTO orderClothItemOutFactoryQueryDTO) {
        return baseMapper.queryWaitFactoryOrderClothAdjunctList(orderClothItemOutFactoryQueryDTO);
    }

    @Override
    public void outFactory(OrderClothItemOutFactoryDTO orderClothItemOutFactoryDTO) {
        /**
         * 先进行验证，如果这批ID里，有状态不符合的，查出来
         */
        List<OrderClothAdjunct> orderClothItems = list(new QueryWrapper<OrderClothAdjunct>().lambda()
                .in(OrderClothAdjunct::getId, orderClothItemOutFactoryDTO.getIds())
                .ne(OrderClothAdjunct::getFlowStatus, ClothOrderFlowStatus.checkCloth.getCode()));
        if (orderClothItems.size() > 0) {
            String errorMsg = "";
            for (OrderClothAdjunct orderClothItem : orderClothItems) {
                errorMsg += (StringUtils.isEmpty(errorMsg)
                        ? ("附件编号" + orderClothItem.getAdjunctNo()) : ("，" + orderClothItem.getAdjunctNo()));
            }
            errorMsg += "洗衣状态不正确，请重新选择";
            throw new ServiceException(errorMsg);
        }
        update(new UpdateWrapper<OrderClothAdjunct>().lambda()
                .set(OrderClothAdjunct::getFlowStatus
                        , ClothOrderFlowStatus.outFactory.getCode())
                .set(OrderClothAdjunct::getOutFactoryTime, DateUtils.getNowDate())
                .set(OrderClothAdjunct::getOutFactoryById, AuthHolder.userId())
                .set(OrderClothAdjunct::getOutFactoryBy, AuthHolder.nickName())
                .in(OrderClothAdjunct::getId, orderClothItemOutFactoryDTO.getIds())
                .eq(OrderClothAdjunct::getFlowStatus, ClothOrderFlowStatus.checkCloth.getCode()));
        //添加记录
        List<OrderClothAdjunct> orderClothAdjunctList = list(new QueryWrapper<OrderClothAdjunct>().lambda()
                .in(OrderClothAdjunct::getId, orderClothItemOutFactoryDTO.getIds()));
        for (OrderClothAdjunct orderClothAdjunct : orderClothAdjunctList) {
            orderClothItemFlowRecordService.addAdjunctRecord(orderClothAdjunct, ClothOrderFlowStatus.outFactory.getCode());
        }
    }
}
