<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.signage.mapper.WorkMapper">

  <resultMap id="workMap" type="com.zioinfo.signage.domain.WorkHistoryVo">
    <id property="id"           column="id"/>
    <result property="tenantCode"   column="tenant_code"/>
    <result property="storeId"      column="store_id"/>
    <result property="storeName"    column="store_name"/>
    <result property="workType"     column="work_type"/>
    <result property="workContent"  column="work_content"/>
    <result property="workerName"   column="worker_name"/>
    <result property="status"       column="status"/>
    <result property="remarks"      column="remarks"/>
    <result property="startedAt"    column="started_at"/>
    <result property="completedAt"  column="completed_at"/>
    <result property="createdAt"    column="created_at"/>
  </resultMap>

  <select id="findAll" resultMap="workMap">
    SELECT w.id, w.tenant_code, w.store_id, s.store_name, w.work_type, w.work_content,
           w.worker_name, w.status, w.remarks, w.started_at, w.completed_at, w.created_at
    FROM signage_work_history w
    LEFT JOIN signage_store s ON w.store_id = s.id
    <where>
      <if test="tenantCode != null and tenantCode != ''">AND w.tenant_code = #{tenantCode}</if>
      <if test="storeId != null">AND w.store_id = #{storeId}</if>
      <if test="status != null and status != ''">AND w.status = #{status}</if>
      <if test="workType != null and workType != ''">AND w.work_type = #{workType}</if>
    </where>
    ORDER BY w.created_at DESC LIMIT 200
  </select>

  <select id="findById" resultMap="workMap">
    SELECT w.id, w.tenant_code, w.store_id, s.store_name, w.work_type, w.work_content,
           w.worker_name, w.status, w.remarks, w.started_at, w.completed_at, w.created_at
    FROM signage_work_history w
    LEFT JOIN signage_store s ON w.store_id = s.id
    WHERE w.id = #{id}
  </select>

  <insert id="insert" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO signage_work_history (tenant_code, store_id, work_type, work_content,
                                   worker_name, status, remarks, started_at)
    VALUES (#{tenantCode}, #{storeId}, #{workType}, #{workContent},
            #{workerName}, #{status}, #{remarks}, #{startedAt})
  </insert>

  <update id="update">
    UPDATE signage_work_history SET
      work_type    = #{workType},
      work_content = #{workContent},
      worker_name  = #{workerName},
      status       = #{status},
      remarks      = #{remarks},
      started_at   = #{startedAt},
      completed_at = #{completedAt}
    WHERE id = #{id}
  </update>

  <delete id="delete">DELETE FROM signage_work_history WHERE id = #{id}</delete>

  <select id="countToday" resultType="long">
    SELECT COUNT(*) FROM signage_work_history WHERE created_at::date = CURRENT_DATE
    <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
  </select>

  <select id="countCompletedToday" resultType="long">
    SELECT COUNT(*) FROM signage_work_history
    WHERE status = 'COMPLETED' AND completed_at::date = CURRENT_DATE
    <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
  </select>

</mapper>
