package com.yiqi.core.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.enums.ClothOrderFlowStatus;
import com.yiqi.common.exception.GlobalException;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.ClothCustomItem;
import com.yiqi.core.domain.vo.ClothHangerOrderRelationVO;
import com.yiqi.core.service.IClothCustomItemService;
import com.yiqi.order.domain.OrderClothItem;
import com.yiqi.order.service.IOrderClothItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.core.mapper.ClothHangerOrderRelationMapper;
import com.yiqi.core.domain.ClothHangerOrderRelation;
import com.yiqi.core.service.IClothHangerOrderRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 洗衣订单衣服挂架位置关系Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-06
 */
@Service
public class ClothHangerOrderRelationServiceImpl extends ServiceImpl<ClothHangerOrderRelationMapper,ClothHangerOrderRelation> implements IClothHangerOrderRelationService {
    @Autowired
    IOrderClothItemService orderClothItemService;

    /**
     * 查询洗衣订单衣服挂架位置关系
     *
     * @param id 洗衣订单衣服挂架位置关系主键
     * @return 洗衣订单衣服挂架位置关系
     */
    @Override
    public ClothHangerOrderRelation selectClothHangerOrderRelationById(Long id) {
        return baseMapper.selectClothHangerOrderRelationById(id);
    }

    /**
     * 查询洗衣订单衣服挂架位置关系列表
     *
     * @param clothHangerOrderRelation 洗衣订单衣服挂架位置关系
     * @return 洗衣订单衣服挂架位置关系
     */
    @Override
    public List<ClothHangerOrderRelation> selectClothHangerOrderRelationList(ClothHangerOrderRelation clothHangerOrderRelation) {
        return baseMapper.selectClothHangerOrderRelationList(clothHangerOrderRelation);
    }

    /**
     * 新增洗衣订单衣服挂架位置关系
     *
     * @param clothHangerOrderRelation 洗衣订单衣服挂架位置关系
     * @return 结果
     */
    @Override
    public int insertClothHangerOrderRelation(ClothHangerOrderRelation clothHangerOrderRelation) {
        clothHangerOrderRelation.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertClothHangerOrderRelation(clothHangerOrderRelation);
    }

    /**
     * 修改洗衣订单衣服挂架位置关系
     *
     * @param clothHangerOrderRelation 洗衣订单衣服挂架位置关系
     * @return 结果
     */
    @Override
    public int updateClothHangerOrderRelation(ClothHangerOrderRelation clothHangerOrderRelation) {
        clothHangerOrderRelation.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateClothHangerOrderRelation(clothHangerOrderRelation);
    }

    /**
     * 批量删除洗衣订单衣服挂架位置关系
     *
     * @param ids 需要删除的洗衣订单衣服挂架位置关系主键
     * @return 结果
     */
    @Override
    public int deleteClothHangerOrderRelationByIds(Long[] ids) {
        return baseMapper.deleteClothHangerOrderRelationByIds(ids);
    }

    /**
     * 删除洗衣订单衣服挂架位置关系信息
     *
     * @param id 洗衣订单衣服挂架位置关系主键
     * @return 结果
     */
    @Override
    public int deleteClothHangerOrderRelationById(Long id) {
        return baseMapper.deleteClothHangerOrderRelationById(id);
    }

    @Override
    public void buildClothHangerOrderRelation(OrderClothItem orderClothItem, Long hangerId, String hangerCode) {
        if (ClothOrderFlowStatus.hangering.getCode().equals(orderClothItem.getFlowStatus())){
            this.update(new UpdateWrapper<ClothHangerOrderRelation>().lambda()
                    .eq(ClothHangerOrderRelation::getOrderClothItemId, orderClothItem.getId())
                    .eq(ClothHangerOrderRelation::getWashCode, orderClothItem.getWashCode())
                    .set(ClothHangerOrderRelation::getClothHangerId, hangerId)
                    .set(ClothHangerOrderRelation::getClothHangerCode, hangerCode));
            return;
        }
        ClothHangerOrderRelation clothHangerOrderRelation = new ClothHangerOrderRelation();
        clothHangerOrderRelation.buildCreateData();
        clothHangerOrderRelation.setClothHangerId(hangerId);
        clothHangerOrderRelation.setClothHangerCode(hangerCode);
        clothHangerOrderRelation.setOrderId(orderClothItem.getOrderId());
        clothHangerOrderRelation.setOrderClothItemId(orderClothItem.getId());
        clothHangerOrderRelation.setWashCode(orderClothItem.getWashCode());
        clothHangerOrderRelation.setWashCode(orderClothItem.getWashCode());
        baseMapper.insert(clothHangerOrderRelation);
    }


}
