package com.njcn.gather.aireport.testdevice.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.gather.aireport.testdevice.pojo.param.TestDeviceParam;
import com.njcn.gather.aireport.testdevice.pojo.po.TestDevicePO;
import com.njcn.gather.aireport.testdevice.pojo.vo.TestDeviceTypeNameVO;
import com.njcn.gather.aireport.testdevice.pojo.vo.TestDeviceVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TestDeviceMapper extends BaseMapper<TestDevicePO> {

    @Select({
            "<script>",
            "select d.id, d.type, dt.name as typeName, d.no, d.validity_period as validityPeriod,",
            "d.validity_report as validityReport, d.state, d.status, d.create_by as createBy,",
            "creator.name as createByName, d.create_time as createTime, d.update_by as updateBy,",
            "updater.name as updateByName, d.update_time as updateTime",
            "from ai_testdevice d",
            "left join cs_dev_type dt on d.type = dt.id",
            "left join sys_user creator on d.create_by = creator.id",
            "left join sys_user updater on d.update_by = updater.id",
            "where d.status = 1",
            "<if test='param.keyword != null and param.keyword != \"\"'>",
            "and (d.no like concat('%', #{param.keyword}, '%') or dt.name like concat('%', #{param.keyword}, '%'))",
            "</if>",
            "<if test='param.searchBeginTime != null and param.searchBeginTime != \"\"'>",
            "and d.create_time &gt;= #{param.searchBeginTime}",
            "</if>",
            "<if test='param.searchEndTime != null and param.searchEndTime != \"\"'>",
            "and d.create_time &lt;= #{param.searchEndTime}",
            "</if>",
            "order by d.create_time desc",
            "</script>"
    })
    Page<TestDeviceVO> selectDevicePage(Page<TestDeviceVO> page, @Param("param") TestDeviceParam.QueryParam param);

    @Select({
            "select d.id, d.type, dt.name as typeName, d.no, d.validity_period as validityPeriod,",
            "d.validity_report as validityReport, d.state, d.status, d.create_by as createBy,",
            "creator.name as createByName, d.create_time as createTime, d.update_by as updateBy,",
            "updater.name as updateByName, d.update_time as updateTime",
            "from ai_testdevice d",
            "left join cs_dev_type dt on d.type = dt.id",
            "left join sys_user creator on d.create_by = creator.id",
            "left join sys_user updater on d.update_by = updater.id",
            "where d.id = #{id} and d.status = 1"
    })
    TestDeviceVO selectDeviceDetail(@Param("id") String id);

    @Select("select count(1) from cs_dev_type where id = #{typeId} and State = 1")
    int countNormalDeviceType(@Param("typeId") String typeId);

    @Select("select id, name from cs_dev_type where State = 1")
    List<TestDeviceTypeNameVO> selectNormalDeviceTypes();
}
