package thyyxxk.webserver.dao.his.medicine;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
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.YpTempPurchase;
import thyyxxk.webserver.entity.medicine.YpTempPurchaseVo;

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

@Mapper
public interface YpTempPurchaseDao extends BaseMapper<YpTempPurchase> {

    @Select(" select zd.code as chargeCode, zd.name as chargeName, zd.serial, zd.specification, zd.retprice, isnull(yc.name, '') as manuFactory " +
            " from yp_zd_dict as zd " +
            " left join yp_zd_manufactory as yc on zd.manu_code = yc.code " +
            " where zd.temp_purchase_flag = '1' " +
            " and (zd.name like '%${text}%' " +
            " or zd.code like '%${text}%' " +
            " or zd.py_code like ('%' + upper(#{text}) + '%') " +
            " or zd.d_code like ('%' + upper(#{text}) + '%')) " +
            " ")
    List<Map<String, Object>> selectTempPurchaseYp(@Param("text") String text);

    @Select("<script>" +
            " select temp.* from yp_temp_purchase as temp " +
            " <where> " +
            "<if test=\" inpatientNo != null and inpatientNo != '' \">" +
            " and (temp.name like '%${inpatientNo}%' or temp.inpatient_no like '%${inpatientNo}%' ) " +
            "</if>" +
            "<if test=\" chargeCode != null and chargeCode != '' \">" +
            " and temp.charge_code = #{chargeCode} " +
            "</if>" +
            "<if test=\" opId != null and opId != '' \">" +
            " and temp.op_id = #{opId} " +
            "</if>" +
            "<if test=\" startTime != null and startTime != '' \">" +
            " and temp.apply_date &gt;= #{startTime} " +
            "</if>" +
            "<if test=\" endTime != null and endTime != '' \">" +
            " and temp.apply_date &lt;= #{endTime} " +
            "</if>" +
            "<if test=\" ztFlag == '1'.toString \">" +
            " and temp.recept_flag = '1' " +
            "</if>" +
            "<if test=\" ztFlag == '2'.toString \">" +
            " and temp.recept_flag != '1' " +
            "</if>" +
            "<if test=\" ztFlag == '3'.toString \">" +
            " and temp.purchase_flag = '1' " +
            "</if>" +
            "<if test=\" ztFlag == '4'.toString \">" +
            " and temp.purchase_flag != '1' " +
            "</if>" +
            "<if test=\" ztFlag == '5'.toString \">" +
            " and temp.storage_flag = '1' " +
            "</if>" +
            "<if test=\" ztFlag == '6'.toString \">" +
            " and temp.storage_flag != '1' " +
            "</if>" +
            " </where> " +
            " order by temp.apply_date " +
            "</script>")
    List<YpTempPurchase> selectYpTempPurchase(YpTempPurchaseVo vo);

    @Select(" select * from yp_temp_purchase where apply_id = #{applyId} ")
    YpTempPurchase selectYpTempPurchaseByApplyId(@Param("applyId") String applyId);

    @Update("<script>" +
            "update yp_temp_purchase " +
            "<trim prefix=\"set\" suffixOverrides=\",\">" +
            "<if test=\"chargeCode != null and chargeCode != '' \">" +
            "charge_code = #{chargeCode}, " +
            "</if>" +
            "<if test=\"chargeName != null and chargeName != '' \">" +
            "charge_name = #{chargeName}, " +
            "</if>" +
            "<if test=\"serial != null and serial != '' \">" +
            "serial = #{serial}, " +
            "</if>" +
            "<if test=\"specification != null and specification != '' \">" +
            "specification = #{specification}, " +
            "</if>" +
            "<if test=\"opId != null and opId != '' \">" +
            "op_id = #{opId}, " +
            "</if>" +
            "<if test=\"op != null and op != '' \">" +
            "op = #{op}, " +
            "</if>" +
            "<if test=\"name != null and name != '' \">" +
            "name = #{name}, " +
            "</if>" +
            "<if test=\"inpatientNo != null and inpatientNo != '' \">" +
            "inpatient_no = #{inpatientNo}, " +
            "</if>" +
            "<if test=\"admissTimes != null and admissTimes != '' \">" +
            "admiss_times = #{admissTimes}, " +
            "</if>" +
            "<if test=\"useResult != null and useResult != '' \">" +
            "use_result = #{useResult}, " +
            "</if>" +
            "<if test=\"receptFlag != null and receptFlag != '' \">" +
            "recept_flag = #{receptFlag}, " +
            "</if>" +
            "<if test=\"purchaseFlag != null and purchaseFlag != '' \">" +
            "purchase_flag = #{purchaseFlag}, " +
            "</if>" +
            "<if test=\"storageFlag != null and storageFlag != '' \">" +
            "storage_flag = #{storageFlag}, " +
            "</if>" +
            "<if test=\"applyDate != null and applyDate != '' \">" +
            "apply_date = #{applyDate}, " +
            "</if>" +
            "<if test=\"auditDate != null and auditDate != '' \">" +
            "audit_date = #{auditDate}, " +
            "</if>" +
            "<if test=\"amount != null and amount != '' \">" +
            "amount = #{amount}, " +
            "</if>" +
            "<if test=\"result != null and result != '' \">" +
            "result = #{result}, " +
            "</if>" +
            "</trim>" +
            "where apply_id = #{applyId} " +
            "</script>")
    int updateYpTempPurchaseByApplyId(YpTempPurchase ypTempPurchase);

    @Update("<script>" +
            "update yp_temp_purchase " +
            "<trim prefix=\"set\" suffixOverrides=\",\">" +
            "<if test=\"receptFlag != null and receptFlag != '' \">" +
            "recept_flag = #{receptFlag}, " +
            "</if>" +
            "<if test=\"purchaseFlag != null and purchaseFlag != '' \">" +
            "purchase_flag = #{purchaseFlag}, " +
            "</if>" +
            "<if test=\"storageFlag != null and storageFlag != '' \">" +
            "storage_flag = #{storageFlag}, " +
            "</if>" +
            "<if test=\"auditDate != null and auditDate != '' \">" +
            "audit_date = #{auditDate}, " +
            "</if>" +
            "<if test=\"result != null and result != '' \">" +
            "result = #{result}, " +
            "</if>" +
            "<if test=\"auditId != null \">" +
            "audit_id = #{auditId}, " +
            "</if>" +
            "<if test=\"auditName != null \">" +
            "audit_name = #{auditName}, " +
            "</if>" +
            "<if test=\"purchaseId != null \">" +
            "purchase_id = #{purchaseId}, " +
            "</if>" +
            "<if test=\"purchaseName != null \">" +
            "purchase_name = #{purchaseName}, " +
            "</if>" +
            "<if test=\"storageId != null \">" +
            "storage_id = #{storageId}, " +
            "</if>" +
            "<if test=\"storageName != null \">" +
            "storage_name = #{storageName}, " +
            "</if>" +
            "</trim>" +
            "where apply_id = #{applyId} " +
            "</script>")
    int updateYpTempPurchaseFlagByApplyId(YpTempPurchase ypTempPurchase);

    @Select(" select count(1) as count from sys_parameters where code = 'tempPurchaseYpAudit' and name like '%${code}%' ")
    int selectAuditTempPurchaseOp(String code);

}
