package thyyxxk.webserver.dao.his.medicine;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.medicine.YpCodgUpload;
import thyyxxk.webserver.entity.medicine.YpSelinfoSold;

import java.util.List;

@Mapper
public interface YpSelinfoSoldDao extends BaseMapper<YpSelinfoSold> {

    @Delete("<script> " +
            " <foreach collection='list' item='item' separator=';'> " +
            "   delete from yp_selinfo_sold where pat_no = #{item.patNo} and times = #{item.times} " +
            "   and order_no = #{item.orderNo} and item_no = #{item.itemNo} " +
            " </foreach> " +
            "</script>")
    void deleteYpSelinfoSoldBatchByIds(List<YpSelinfoSold> list);

    @Delete("<script> " +
            " <foreach collection='list' item='item' separator=';'> " +
            "   delete from yp_selinfo_sold where pat_no = #{item.patNo} and times = #{item.times} " +
            "   and order_no = #{item.orderNo} and item_no = #{item.itemNo} " +
            " </foreach> " +
            "</script>")
    int deleteYpSelinfoSoldBatchByCodes(List<YpCodgUpload> list);

    @Update("<script> " +
            " <foreach collection='list' item='item' separator=';'> " +
            "   update yp_selinfo_sold set upload_flag = '-2' where pat_no = #{item.patNo} and times = #{item.times} " +
            "   and receipt_no = #{item.receiptNo} and order_no = #{item.orderNo} and item_no = #{item.itemNo} " +
            " </foreach> " +
            "</script>")
    int updateYpSelinfoSoldBatchByCode(List<YpCodgUpload> list);

    @Insert("<script> " +
            " insert into yp_selinfo_sold (med_list_codg, fixmedins_hilist_id, fixmedins_hilist_name, fixmedins_bchno, " +
            " prsc_dr_cert_type, prsc_dr_certno, prsc_dr_name, phar_cert_type, phar_certno, phar_name, phar_prac_cert_no, " +
            " hi_feesetl_type, setl_id, mdtrt_sn, psn_no, psn_cert_type, certno, psn_name, manu_lotnum, manu_date, expy_end, " +
            " rx_flag, trdn_flag, finl_trns_pric, rxno, rx_circ_flag, rtal_docno, stoout_no, bchno, drug_trac_codg, drug_prod_barc, " +
            " shelf_posi, sel_retn_cnt, sel_retn_time, sel_retn_opter_name, memo, charge_name, serial, upload_flag, msg_err, pat_no, " +
            " times, group_no, receipt_no, order_no, item_no, serial_no, real_no, create_time, is_ty, charge_code, " +
            " prsc_dr_no, phar_no, MDTRT_SETL_TYPE) values " +
            " <foreach collection='list' item='item'  separator=','> " +
            " (#{item.medListCodg},#{item.fixmedinsHilistId},#{item.fixmedinsHilistName},#{item.fixmedinsBchno},#{item.prscDrCertType},#{item.prscDrCertno},#{item.prscDrName}," +
            " #{item.pharCertType},#{item.pharCertno},#{item.pharName},#{item.pharPracCertNo},#{item.hiFeesetlType},#{item.setlId},#{item.mdtrtSn},#{item.psnNo},#{item.psnCertType}," +
            " #{item.certno},#{item.psnName},#{item.manuLotnum},#{item.manuDate},#{item.expyEnd},#{item.rxFlag},#{item.trdnFlag},#{item.finlTrnsPric},#{item.rxno},#{item.rxCircFlag}," +
            " #{item.rtalDocno},#{item.stooutNo},#{item.bchno},#{item.drugTracCodg},#{item.drugProdBarc},#{item.shelfPosi},#{item.selRetnCnt},#{item.selRetnTime},#{item.selRetnOpterName}," +
            " #{item.memo},#{item.chargeName},#{item.serial},#{item.uploadFlag},#{item.msgErr},#{item.patNo},#{item.times},#{item.groupNo},#{item.receiptNo},#{item.orderNo},#{item.itemNo}," +
            " #{item.serialNo},#{item.realNo},#{item.createTime},#{item.isTy},#{item.chargeCode}," +
            " #{item.prscDrNo},#{item.pharNo},#{item.mdtrtSetlType}) " +
            " </foreach> " +
            " </script>")
    int saveYpSelinfoSoldBatch(List<YpSelinfoSold> list);

    @Select("<script> " +
            " select so.* from yp_selinfo_sold as so " +
            " where so.sel_retn_time &gt;= #{dto.startTime} " +
            " and so.sel_retn_time &lt;= #{dto.endTime} " +
            " and so.is_ty = '0' " +
            " and so.group_no = #{dto.groupNo}  " +
            " <if test=\"dto.patNo != null and dto.patNo != ''\"> " +
            " and so.pat_no = #{dto.patNo} " +
            " </if> " +
            " <if test=\"dto.chargeCode != null and dto.chargeCode != ''\"> " +
            " and so.charge_code = #{dto.chargeCode} " +
            " </if> " +
            " <if test=\"dto.setlId != null and dto.setlId != ''\"> " +
            " and so.setl_id = #{dto.setlId} " +
            " </if> " +
            " <if test=\"dto.uploadFlag != null and dto.uploadFlag != ''\"> " +
            " and so.upload_flag = #{dto.uploadFlag} " +
            " </if> " +
            " order by so.sel_retn_time, so.pat_no " +
            "</script>")
    List<YpSelinfoSold> selectYpSelinfoSoldResult(@Param("dto") YpCodgUpload dto);

    @Select("<script> " +
            " select bo.* from yp_selinfo_sold as bo " +
            " where bo.sel_retn_time &gt;= #{dto.startTime} " +
            " and bo.sel_retn_time &lt;= #{dto.endTime} " +
            " and bo.is_ty = '1' " +
            " and bo.group_no = #{dto.groupNo}  " +
            " <if test=\"dto.patNo != null and dto.patNo != ''\"> " +
            " and bo.pat_no = #{dto.patNo} " +
            " </if> " +
            " <if test=\"dto.chargeCode != null and dto.chargeCode != ''\"> " +
            " and bo.charge_code = #{dto.chargeCode} " +
            " </if> " +
            " <if test=\"dto.setlId != null and dto.setlId != ''\"> " +
            " and bo.setl_id = #{dto.setlId} " +
            " </if> " +
            " <if test=\"dto.uploadFlag != null and dto.uploadFlag != ''\"> " +
            " and bo.upload_flag = #{dto.uploadFlag} " +
            " </if> " +
            " order by bo.sel_retn_time, bo.pat_no " +
            "</script>")
    List<YpSelinfoSold> selectYpSelinfoSoldTkResult(@Param("dto") YpCodgUpload dto);

}
