package com.cf.imes.module.executor.dal.mysql.remainplaten;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.module.executor.controller.admin.remainplate.vo.RemainPlatePageReqVO;
import com.cf.imes.module.executor.dal.dataobject.remainplaten.RemainPlateDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getUserOrganId;

@Mapper
public interface RemainPlateMapper extends BaseMapperX<RemainPlateDO> {

    default PageResult<RemainPlateDO> selectPage(RemainPlatePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RemainPlateDO>()
                .eq(RemainPlateDO::getOrganId,getUserOrganId())
                .eqIfPresent(RemainPlateDO::getPlanId, reqVO.getPlanId())
                .eqIfPresent(RemainPlateDO::getInitPlanId, reqVO.getInitPlanId())
                .eqIfPresent(RemainPlateDO::getStatus, reqVO.getStatus())
                .eqIfPresent(RemainPlateDO::getGoodsId, reqVO.getGoodsId())
                .likeIfPresent(RemainPlateDO::getName, reqVO.getName())
                .eqIfPresent(RemainPlateDO::getMaterial, reqVO.getMaterial())
                .eqIfPresent(RemainPlateDO::getColor, reqVO.getColor())
                .eqIfPresent(RemainPlateDO::getWidth, reqVO.getWidth())
                .eqIfPresent(RemainPlateDO::getLength, reqVO.getLength())
                .eqIfPresent(RemainPlateDO::getThickness, reqVO.getThickness())
                .eqIfPresent(RemainPlateDO::getBrand, reqVO.getBrand())
                .eqIfPresent(RemainPlateDO::getPlaceStyle, reqVO.getPlaceStyle())
                .eqIfPresent(RemainPlateDO::getStore, reqVO.getStore())
                .eqIfPresent(RemainPlateDO::getCount, reqVO.getCount())
                .eqIfPresent(RemainPlateDO::getRemark, reqVO.getRemark())
                .eqIfPresent(RemainPlateDO::getOutLineJson, reqVO.getOutline())
                .betweenIfPresent(RemainPlateDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RemainPlateDO::getId));
    }


    default List<RemainPlateDO> selectByPlanId(Long planId, Long organId) {
        return selectList( new LambdaQueryWrapperX<RemainPlateDO>()
                .eq(RemainPlateDO::getOrganId, organId)
                .eq(RemainPlateDO::getDeleted,false)
                .eq(RemainPlateDO::getPlanId, planId));


    }



    // 批量修改删除状态
    default int updateOrderDeleted(Collection<Long> ids, Integer deleted, Long organId) {
        return update(new LambdaUpdateWrapper<RemainPlateDO>()
                .set(RemainPlateDO::getDeleted, deleted)
                .in(RemainPlateDO::getId, ids)
                .eq(RemainPlateDO::getOrganId, organId));
    }

    List<RemainPlateDO> selectPlanRemainDataSource(@Param("filed") String filed,@Param("planId")  Long planId,@Param("organId")  Long organId);



    default int deletePlanRemainPlate(List<Long> ids, Long organId) {
        return update(new LambdaUpdateWrapper<RemainPlateDO>()
                .set(RemainPlateDO::getPlanId, 0)
                .eq(RemainPlateDO::getOrganId, organId)
                .in(RemainPlateDO::getPlanId, ids));
    }


    default PageResult<RemainPlateDO> selectPage(com.cf.imes.module.executor.controller.admin.manage.plate.vo.remain.RemainPlatePageReqVO reqVO) {
        LambdaQueryWrapperX<RemainPlateDO> wrapper = new LambdaQueryWrapperX<RemainPlateDO>()
                .likeIfPresent(RemainPlateDO::getId,reqVO.getId() != null ? reqVO.getId() : null)
                .likeIfPresent(RemainPlateDO::getPlanId, reqVO.getPlanId() != null ? reqVO.getPlanId() : null)
                .likeIfPresent(RemainPlateDO::getInitPlanId, reqVO.getInitPlanId() != null ? reqVO.getInitPlanId() : null)
                .eqIfPresent(RemainPlateDO::getStatus, reqVO.getStatus())
                .eqIfPresent(RemainPlateDO::getGoodsId, reqVO.getGoodsId())
                .eqIfPresent(RemainPlateDO::getIsCutting, reqVO.getIsCutting())
                .likeIfPresent(RemainPlateDO::getName, reqVO.getName())
                .likeIfPresent(RemainPlateDO::getMaterial, reqVO.getMaterial())
                .likeIfPresent(RemainPlateDO::getColor, reqVO.getColor())
                .eqIfPresent(RemainPlateDO::getThickness, reqVO.getThickness())
                .eqIfPresent(RemainPlateDO::getBrand, reqVO.getBrand())
                .eqIfPresent(RemainPlateDO::getPlaceStyle, reqVO.getPlaceStyle())
                .likeIfPresent(RemainPlateDO::getStore, reqVO.getStore())
                .eqIfPresent(RemainPlateDO::getCount, reqVO.getCount())
                .eqIfPresent(RemainPlateDO::getTexture, reqVO.getTexture())
                .likeIfPresent(RemainPlateDO::getRemark, reqVO.getRemark())
                .eqIfPresent(RemainPlateDO::getUseType, reqVO.getUseType())
                .eqIfPresent(RemainPlateDO::getOrganId, reqVO.getOrganId())
                .eqIfPresent(RemainPlateDO::getOutLineJson, reqVO.getOutLineJson())
                .betweenIfPresent(RemainPlateDO::getWidth, reqVO.getWidth())
                .betweenIfPresent(RemainPlateDO::getLength, reqVO.getLength())
                .betweenIfPresent(RemainPlateDO::getCreateTime, reqVO.getCreateTime());

        if (reqVO.getOrder() == null) {
            wrapper.orderByDesc(RemainPlateDO::getId);
        }else {
            if (reqVO.getOrder().equals("ascend")) {
                switch (reqVO.getField()) {
                    case "id" -> wrapper.orderByAsc(RemainPlateDO::getId);
                    case "length" -> wrapper.orderByAsc(RemainPlateDO::getLength);
                    case "width" -> wrapper.orderByAsc(RemainPlateDO::getWidth);
                    case "thickness" -> wrapper.orderByAsc(RemainPlateDO::getThickness);
                    case "material" -> wrapper.orderByAsc(RemainPlateDO::getMaterial);
                    case "color" -> wrapper.orderByAsc(RemainPlateDO::getColor);
                    case "texture" -> wrapper.orderByAsc(RemainPlateDO::getTexture);
                    case "brand" -> wrapper.orderByAsc(RemainPlateDO::getBrand);
                    case "store" -> wrapper.orderByAsc(RemainPlateDO::getStore);
                    case "initPlanId" -> wrapper.orderByAsc(RemainPlateDO::getInitPlanId);
                    case "planId" -> wrapper.orderByAsc(RemainPlateDO::getPlanId);
                    case "status" -> wrapper.orderByAsc(RemainPlateDO::getStatus);
                    default -> {
                        // default不排序
                    }
                }
            }else if (reqVO.getOrder().equals("descend")) {
                switch (reqVO.getField()) {
                    case "id" -> wrapper.orderByDesc(RemainPlateDO::getId);
                    case "length" -> wrapper.orderByDesc(RemainPlateDO::getLength);
                    case "width" -> wrapper.orderByDesc(RemainPlateDO::getWidth);
                    case "thickness" -> wrapper.orderByDesc(RemainPlateDO::getThickness);
                    case "material" -> wrapper.orderByDesc(RemainPlateDO::getMaterial);
                    case "color" -> wrapper.orderByDesc(RemainPlateDO::getColor);
                    case "texture" -> wrapper.orderByDesc(RemainPlateDO::getTexture);
                    case "brand" -> wrapper.orderByDesc(RemainPlateDO::getBrand);
                    case "store" -> wrapper.orderByDesc(RemainPlateDO::getStore);
                    case "initPlanId" -> wrapper.orderByDesc(RemainPlateDO::getInitPlanId);
                    case "planId" -> wrapper.orderByDesc(RemainPlateDO::getPlanId);
                    case "status" -> wrapper.orderByDesc(RemainPlateDO::getStatus);
                    default -> {
                        // default不排序
                    }
                }
            }
        }

        return selectPage(reqVO, wrapper);
    }

    default PageResult<RemainPlateDO> selectPageToAdd(com.cf.imes.module.executor.controller.admin.manage.plate.vo.remain.RemainPlatePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<RemainPlateDO>()
                .neIfPresent(RemainPlateDO::getInitPlanId, reqVO.getInitPlanId())
                .eqIfPresent(RemainPlateDO::getStatus, reqVO.getStatus())
                .eqIfPresent(RemainPlateDO::getGoodsId, reqVO.getGoodsId())
                .likeIfPresent(RemainPlateDO::getName, reqVO.getName())
                .likeIfPresent(RemainPlateDO::getMaterial, reqVO.getMaterial())
                .likeIfPresent(RemainPlateDO::getColor, reqVO.getColor())
                .eqIfPresent(RemainPlateDO::getThickness, reqVO.getThickness())
                .eqIfPresent(RemainPlateDO::getBrand, reqVO.getBrand())
                .likeIfPresent(RemainPlateDO::getStore, reqVO.getStore())
                .eqIfPresent(RemainPlateDO::getCount, reqVO.getCount())
                .eqIfPresent(RemainPlateDO::getTexture, reqVO.getTexture())
                .likeIfPresent(RemainPlateDO::getRemark, reqVO.getRemark())
                .eqIfPresent(RemainPlateDO::getOrganId, reqVO.getOrganId())
                .eqIfPresent(RemainPlateDO::getOutLineJson, reqVO.getOutLineJson())
                .betweenIfPresent(RemainPlateDO::getWidth, reqVO.getWidth())
                .betweenIfPresent(RemainPlateDO::getLength, reqVO.getLength())
                .betweenIfPresent(RemainPlateDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(RemainPlateDO::getId));
    }
    // 批量修改状态
    default void updateRemainPlateStatus(Set<Long> ids, Integer status, Integer useType, Long organId, Long planId) {
        update(new UpdateWrapper<RemainPlateDO>()
                .set("status", status)
                .set("use_type", useType)
                .set("plan_id",planId)
                .eq("organ_id",organId)
                .in("id", ids));
    }

    // 查询该排单是否已经添加余料
    default List<RemainPlateDO> selectRemainPlatesByInitPlanId(Long initPlanId, Integer deleted, Long organId) {
        return selectList(new LambdaUpdateWrapper<RemainPlateDO>()
                .eq(RemainPlateDO::getInitPlanId, initPlanId)
                .eq(RemainPlateDO::getDeleted, deleted)
                .eq(RemainPlateDO::getOrganId, organId));
    }

    // 判断该排单是否存在以使用的余料
    default List<RemainPlateDO> selectRemainPlatesByPlanId(Long planId, Integer status, Integer deleted, Long organId) {
        return selectList(new LambdaUpdateWrapper<RemainPlateDO>()
                .eq(RemainPlateDO::getPlanId, planId)
                .eq(RemainPlateDO::getStatus, status)
                .eq(RemainPlateDO::getDeleted, deleted)
                .eq(RemainPlateDO::getOrganId, organId));
    }

    // 通过排单id批量余料板
    void deleteRemainPlate(@Param("initPlanId") Long initPlanId, @Param("organId") Long organId);

    // 根据使用排单id批量还原状态
    default void updateRemainPlateStatusByPlanId(Integer status, Integer useType,Long organId,Long planId) {
        update(new UpdateWrapper<RemainPlateDO>()
                .set("status", status)
                .set("use_type", useType)
                .set("plan_id","")
                .eq("organ_id",organId)
                .eq("plan_id", planId));
    }
}
