<?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.ycwl.basic.mapper.PrinterMapper">
    <!-- 查询列表 -->
    <select id="list" parameterType="com.ycwl.basic.model.pc.printer.entity.PrinterEntity" resultType="com.ycwl.basic.model.pc.printer.entity.PrinterEntity">
        SELECT * FROM printer
        <where>
            <if test="condition != null">
                <if test="condition.scenicId != null">
                    AND scenic_id = #{condition.scenicId}
                </if>
                <if test="condition.status != null">
                    AND status = #{condition.status}
                </if>
                <if test="condition.name != null and condition.name != ''">
                    AND name LIKE CONCAT('%', #{condition.name}, '%')
                </if>
            </if>
        </where>
    </select>

    <!-- 获取单条记录 -->
    <select id="getById" resultType="com.ycwl.basic.model.pc.printer.entity.PrinterEntity">
        SELECT * FROM printer WHERE id = #{id}
    </select>
    <select id="findByAccessKey" resultType="com.ycwl.basic.model.pc.printer.entity.PrinterEntity">
        SELECT * FROM printer WHERE access_key = #{accessKey} LIMIT 1
    </select>
    <select id="findTaskByPrinterId" resultType="com.ycwl.basic.model.printer.resp.PrintTaskResp">
        select * FROM print_task WHERE status = 0 and printer_id = #{printerId} LIMIT 1
    </select>
    <select id="getTaskById" resultType="com.ycwl.basic.model.pc.printer.entity.PrintTaskEntity">
        select * from print_task WHERE id = #{id}
    </select>
    <select id="listByScenicId" resultType="com.ycwl.basic.model.pc.printer.resp.PrinterResp">
        SELECT p.*
        FROM printer p
        WHERE p.scenic_id = #{scenicId} and p.status = 1
    </select>
    <select id="listRelation" resultType="com.ycwl.basic.model.pc.printer.resp.MemberPrintResp">
        SELECT p.*
        FROM member_print p
        WHERE p.member_id = #{memberId} AND p.scenic_id = #{scenicId} AND p.status = 0
    </select>
    <select id="listRelationByFaceId" resultType="com.ycwl.basic.model.pc.printer.resp.MemberPrintResp">
        SELECT p.*
        FROM member_print p
        WHERE p.member_id = #{memberId} AND p.scenic_id = #{scenicId} AND p.face_id = #{faceId} AND p.status = 0
    </select>
    <select id="getUserPhoto" resultType="com.ycwl.basic.model.pc.printer.resp.MemberPrintResp">
        SELECT p.*
        FROM member_print p
        WHERE p.id = #{id} AND p.member_id = #{memberId} AND p.scenic_id = #{scenicId}
    </select>
    <select id="getUserPhotoByIds" resultType="com.ycwl.basic.model.pc.printer.resp.MemberPrintResp">
        SELECT p.*
        FROM member_print p
        WHERE p.id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="listRelationByOrderId" resultType="com.ycwl.basic.model.pc.printer.resp.MemberPrintResp">
        SELECT p.*
        FROM member_print p
        WHERE p.id in (select order_item.goods_id from order_item where order_item.order_id = #{orderId} and order_item.goods_type = 3)
    </select>
    <select id="queryTaskByMpId" resultType="com.ycwl.basic.model.printer.resp.PrintTaskResp">
        SELECT t.*, p.name as printerName FROM print_task t left join printer p on p.id = t.printer_id WHERE mp_id = #{orderId} order by t.create_time desc limit 1
    </select>

    <!-- 新增 -->
    <insert id="add">
        INSERT INTO printer (
            access_key,
            name,
            scenic_id,
            printers,
            use_printer,
            status,
            prefer_w,
            prefer_h,
            prefer_paper,
            create_time,
            update_time
        ) VALUES (
            #{accessKey},
            #{name},
            #{scenicId},
            #{printers},
            #{usePrinter},
            #{status},
            #{preferW},
            #{preferH},
            #{preferPaper},
            NOW(),
            NOW()
        )
    </insert>
    <insert id="addUserPhoto" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO member_print (
            member_id,
            scenic_id,
            face_id,
            source_id,
            orig_url,
            crop_url,
            quantity,
            status,
            create_time,
            update_time
        ) VALUES (
            #{memberId},
            #{scenicId},
            #{faceId},
            #{sourceId},
            #{origUrl},
            #{cropUrl},
            1,
            0,
            NOW(),
            NOW()
        )
    </insert>

    <!-- 更新 -->
    <update id="update">
        UPDATE printer
        <set>
            <if test="accessKey != null">
                access_key = #{accessKey},
            </if>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="scenicId != null">
                scenic_id = #{scenicId},
            </if>
            <if test="printers != null">
                printers = #{printers},
            </if>
            <if test="usePrinter != null">
                use_printer = #{usePrinter},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="preferW != null">
                prefer_w = #{preferW},
            </if>
            <if test="preferH != null">
                prefer_h = #{preferH},
            </if>
            <if test="preferPaper != null">
                prefer_paper = #{preferPaper},
            </if>
            update_time = NOW()
        </set>
        WHERE id = #{id}
    </update>
    <update id="updateTaskStatus">
        UPDATE print_task SET status = #{status}, update_time = NOW() WHERE id = #{id}
    </update>
    <update id="compareAndSetTaskStatus">
        UPDATE print_task
        SET status = #{newStatus}, update_time = NOW()
        WHERE id = #{id} AND status = #{expectStatus}
    </update>
    <update id="setPhotoCropped">
        UPDATE member_print SET crop_url = #{url}, crop = #{crop}, update_time = NOW(), print_url = null WHERE id = #{id}
    </update>
    <update id="setPhotoQuantity">
        UPDATE member_print SET quantity = #{quantity}, update_time = NOW() WHERE id = #{id}
    </update>
    <update id="setUserIsBuyItem">
        UPDATE member_print SET order_id = #{orderId}, status = 1, update_time = NOW() WHERE member_id = #{memberId} AND id = #{id}
    </update>
    <update id="updateUserPhotoListToPrinter">
        UPDATE member_print SET printer_id = #{printerId}, update_time = NOW() WHERE member_id = #{memberId} AND scenic_id = #{scenicId} AND status = 0 AND printer_id is null
    </update>

    <!-- 删除 -->
    <delete id="deleteById">
        DELETE FROM printer WHERE id = #{id}
    </delete>
    <delete id="deleteUserPhoto">
        DELETE FROM member_print WHERE member_id = #{memberId} AND scenic_id = #{scenicId} AND id = #{relationId} LIMIT 1;
    </delete>
</mapper>
