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.core.domain.BaseEntity;
import com.yiqi.common.enums.ClothOrderFlowStatus;
import com.yiqi.common.enums.ClothOrderStatusType;
import com.yiqi.common.enums.SysConfigKey;
import com.yiqi.common.exception.GlobalException;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.ClothHanger;
import com.yiqi.core.domain.ClothHangerOrderRelation;
import com.yiqi.core.domain.vo.ClothHangerDetailVO;
import com.yiqi.core.domain.vo.ClothHangerOrderRelationVO;
import com.yiqi.core.service.IClothHangerOrderRelationService;
import com.yiqi.core.service.IClothHangerService;
import com.yiqi.order.domain.OrderCloth;
import com.yiqi.order.domain.OrderClothItem;
import com.yiqi.order.service.IOrderClothItemFlowRecordService;
import com.yiqi.order.service.IOrderClothItemService;
import com.yiqi.order.service.IOrderClothService;
import com.yiqi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.core.mapper.ClothHangerDetailMapper;
import com.yiqi.core.domain.ClothHangerDetail;
import com.yiqi.core.service.IClothHangerDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 挂架位置详情Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-06
 */
@Service
public class ClothHangerDetailServiceImpl extends ServiceImpl<ClothHangerDetailMapper,ClothHangerDetail> implements IClothHangerDetailService {
    @Autowired
    private IClothHangerOrderRelationService clothHangerOrderRelationService;

    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IOrderClothItemService orderClothItemService;

    @Autowired
    private IOrderClothService orderClothService;

    @Autowired
    private IOrderClothItemFlowRecordService orderClothItemFlowRecordService;

    @Autowired
    private IClothHangerService clothHangerService;

    /**
     * 查询挂架位置详情
     *
     * @param id 挂架位置详情主键
     * @return 挂架位置详情
     */
    @Override
    public ClothHangerDetail selectClothHangerDetailById(Long id) {
        return baseMapper.selectClothHangerDetailById(id);
    }

    /**
     * 查询挂架位置详情列表
     *
     * @param clothHangerDetail 挂架位置详情
     * @return 挂架位置详情
     */
    @Override
    public List<ClothHangerDetail> selectClothHangerDetailList(ClothHangerDetail clothHangerDetail) {
        return baseMapper.selectClothHangerDetailList(clothHangerDetail);
    }

    @Override
    public List<ClothHangerDetailVO> getIsHangerList(Long hangerId) {
        return baseMapper.getIsHangerList(hangerId);
    }

    /**
     * 新增挂架位置详情
     *
     * @param clothHangerDetail 挂架位置详情
     * @return 结果
     */
    @Override
    public int insertClothHangerDetail(ClothHangerDetail clothHangerDetail) {
        clothHangerDetail.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertClothHangerDetail(clothHangerDetail);
    }

    /**
     * 修改挂架位置详情
     *
     * @param clothHangerDetail 挂架位置详情
     * @return 结果
     */
    @Override
    public int updateClothHangerDetail(ClothHangerDetail clothHangerDetail) {
        clothHangerDetail.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateClothHangerDetail(clothHangerDetail);
    }

    /**
     * 批量删除挂架位置详情
     *
     * @param ids 需要删除的挂架位置详情主键
     * @return 结果
     */
    @Override
    public int deleteClothHangerDetailByIds(Long[] ids) {
        return baseMapper.deleteClothHangerDetailByIds(ids);
    }

    /**
     * 删除挂架位置详情信息
     *
     * @param id 挂架位置详情主键
     * @return 结果
     */
    @Override
    public int deleteClothHangerDetailById(Long id) {
        return baseMapper.deleteClothHangerDetailById(id);
    }

    @Override
    public List<ClothHangerOrderRelationVO> getClothOrderInfoByHangerCodeAndId(Long hangerId, String hangerCode) {
        return baseMapper.getClothOrderInfoByHangerCodeAndId(hangerId, hangerCode);
    }

    /**
     * 上挂
     * @param orderClothItemId
     * @param hangerId
     * @param hangerCode
     * @return
     */

    @Override
    public int upHanger(Long orderClothItemId, Long hangerId, String hangerCode) {
        OrderClothItem orderClothItem = orderClothItemService.getOne(new QueryWrapper<OrderClothItem>().lambda()
                .eq(OrderClothItem::getId, orderClothItemId));
        if (orderClothItem == null){
            throw new GlobalException("未查询到订单相关衣物信息");
        }
        ClothHanger hanger = clothHangerService.getOne(new QueryWrapper<ClothHanger>().lambda()
                .eq(ClothHanger::getId, hangerId));
        // 查询挂架位置是否满
        long count = clothHangerOrderRelationService.count(new QueryWrapper<ClothHangerOrderRelation>().lambda()
                .eq(ClothHangerOrderRelation::getClothHangerId, hangerId)
                .eq(ClothHangerOrderRelation::getClothHangerCode, hangerCode));
        if (count >= Integer.parseInt(configService.selectConfigByKey(SysConfigKey.SysHangerMaxCount.getCode()))){
            throw new GlobalException("该位置已满，请选择其他位置");
        }
        // 创建上挂关系
        clothHangerOrderRelationService.buildClothHangerOrderRelation(orderClothItem, hangerId, hangerCode);

        // 更新订单衣服挂架位置信息
        orderClothItem.setClothHangerId(hangerId);
        orderClothItem.setClothHangerCode(hangerCode);
        orderClothItem.setFlowStatus(ClothOrderFlowStatus.hangering.getCode());
        orderClothItem.setClothHangerName(hanger.getName()+"-"+hangerCode+"#");
        orderClothItem.buildUpdateData();
        orderClothItemService.updateById(orderClothItem);

        // 当衣服全部上挂后 是否需要自动更新订单状态= 7 待取衣
        //判断所有衣服的状态，更新订单状态
        orderClothService.checkAllOrderStatus(orderClothItem.getOrderId(), ClothOrderStatusType.HAS_WASH.getCode());
        orderClothItemFlowRecordService.addRecord(orderClothItem, orderClothItem.getFlowStatus());
        return 1;
    }


}
