package com.zt.plat.module.qms.resource.device.dal.mapper;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.zt.plat.module.qms.core.aspect.annotation.QmsPermission;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceInfomationPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceInfomationWithProductVO;
import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceConfigBusinessRuleDO;
import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceInfoWithBizConfigVO;
import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceInfomationDO;
import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceProductDO;
import io.swagger.v3.oas.annotations.media.Schema;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Map;

import static com.zt.plat.module.qms.enums.QmsPermissionConstant.DEPT_DATA_AND_SUB;
import static com.zt.plat.module.qms.enums.QmsPermissionConstant.DEVICE_MANAGER;

/**
 * 设备-设备信息 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface DeviceInfomationMapper extends BaseMapperX<DeviceInfomationDO> {

    Page<DeviceInfomationWithProductVO> selectPage(IPage<?> page, @Param("param") DeviceInfomationPageReqVO param);

    //查询需要“某个业务类型”的设备列表
    default List<DeviceInfoWithBizConfigVO> getListNeedByRule(JSONObject param){
        String businessType = param.getString("businessType");
        Long deviceId = param.getLong("deviceId");
        List deptIdList = param.getJSONArray("deptIdList");
        MPJLambdaWrapperX<DeviceInfomationDO> wrapper = new MPJLambdaWrapperX<DeviceInfomationDO>();
        wrapper.leftJoin(DeviceProductDO.class, DeviceProductDO::getId, DeviceInfomationDO::getProductId)
                .leftJoin(DeviceProductDO.class, DeviceProductDO::getId, DeviceInfomationDO::getProductId)
                .selectAssociation(DeviceConfigBusinessRuleDO.class, DeviceInfoWithBizConfigVO::getRule)
                .leftJoin(DeviceConfigBusinessRuleDO.class, DeviceConfigBusinessRuleDO::getProductId, DeviceInfomationDO::getProductId)
                .selectAll(DeviceInfomationDO.class);
        wrapper.eqIfPresent(DeviceInfomationDO::getId, deviceId)
                .inIfPresent(DeviceInfomationDO::getDeptId, deptIdList)
                .eq(DeviceConfigBusinessRuleDO::getBusinessDomain, businessType)
                .eq(DeviceConfigBusinessRuleDO::getRequireFlag, "1");

        return selectJoinList(DeviceInfoWithBizConfigVO.class, wrapper);
    }

}