package thyyxxk.webserver.dao.his.zhuyuanyisheng;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.datamodify.ZyDetailCharge;
import thyyxxk.webserver.entity.zhuyuanyisheng.caoyaoyizhu.YzActOrderCy;
import thyyxxk.webserver.entity.zhuyuanyisheng.caoyaoyizhu.YzActOrderCyDetail;
import thyyxxk.webserver.entity.zhuyuanyisheng.caoyaoyizhu.YzOrderPatternCy;
import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.YiZhuMingChen;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 描述： 草药医嘱
 * </p>
 *
 * @author xc
 * @date 2022-02-26 08:38
 */
@Mapper
public interface CaoYaoYiZhuDao {
    @Select("select order_no," +
            "       inpatient_no," +
            "       admiss_times, " +
            "       order_name, " +
            "       doctor_code_name = (select rtrim(name) from a_employee_mi where code = doctor_code), " +
            "       status_flag /* 1-录入 2-收费 3-提交 4-以发药 d-删除*/, " +
            "       order_je,page_no, " +
            "       order_je_dj, " +
            "       quantity, " +
            "       quantity_dj, " +
            "       order_code_dj," +
            "       cy_jssm," +
            "       cy_mtjs," +
            "       cy_ffcs," +
            "       cy_fysm," +
            "       cy_fysm_name     = (select name from mzysh_zd_fysm where code = cy_fysm)," +
            "       input_date," +
            "       exec_dept, " +
            "       exec_dept_name = (select name from zd_unit_code where code = exec_dept) " +
            "from yz_act_order_cy with (NOLOCK) " +
            "${ew.customSqlSegment} ")
    List<YzActOrderCy> huoQuCaoYaoShuJu(@Param(Constants.WRAPPER) Wrapper<?> wp);


    /**
     * 获取代煎费
     *
     * @return 返回金额
     */
    @Select("select isnull(charge_amount,0) from zd_charge_item " +
            "with (NOLOCK) where code = (select charge_code_zydj from yz_sequence) ")
    BigDecimal huoQuDaiJianFei();


    @Select("select chargeCodeName = (select rtrim(name) from view_yp_zd_dict_base_yf with (NOLOCK) where code = charge_code)," +
            "       group_no_name  = (select rtrim(group_name) " +
            "                         from yp_zd_group_name with (NOLOCK)  " +
            "                         where yz_act_order_cy_detail.group_no = yp_zd_group_name.group_no), " +
            "       * " +
            "from yz_act_order_cy_detail with (NOLOCK) " +
            "where order_no = #{orderNo}")
    List<YzActOrderCyDetail> huoQuChaoYaoMingXi(BigDecimal orderNo);

    @Select("SELECT code, name FROM mzysh_zd_fysm with (NOLOCK)")
    List<GetDropdownBox> fuYongFangFa();


    @Select("SELECT            code as chargeCode, " +
            "                  name as chargeCodeName, " +
            "                  specification, " +
            "                  'z'  as asorderType, " +
            "                  serial, " +
            "                  group_no, " +
            "                  infusion_flag, " +
            "                  drug_flag, " +
            "                  group_name, " +
            "                  pack_size, " +
            "    order_class ='Y', " +
            "    retprice=pack_retprice, " +
            "    self_flag   = isnull(self_flag, '0'), " +
            "                  suprice_flag,bill_item_zy " +
            "FROM view_yp_zd_dict_base_yf with (NOLOCK) " +
            "${ew.customSqlSegment} AND isnull(visible_flag_zy, '') <> '1' ")
    List<YiZhuMingChen> huoQuCaoYao(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select pattern_name, " +
            "       group_no =max(group_no), " +
            "       dept_code=max(dept_code) " +
            "from yz_order_pattern_cy with (NOLOCK) " +
            " ${ew.customSqlSegment} ")
    IPage<YzOrderPatternCy> huoQuMuBan(IPage<YzOrderPatternCy> page,
                                       @Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);


    @Select("SELECT distinct charge_code=a.charge_code, " +
            "                serial=a.serial, " +
            "                group_no=a.group_no, " +
            "                quantity=a.quantity, " +
            "                retprice=b.pack_retprice, " +
            "                supply_code=a.supply_code, " +
            "                instruction=a.instruction, " +
            "                charge_code_name=b.name," +
            "                b.specification, " +
            "                b.self_flag, " +
            "                b.suprice_flag,bill_item_zy " +
            "FROM yz_order_pattern_cy a with (NOLOCK), " +
            "     yp_zd_dict b with (NOLOCK), " +
            "     yp_base_yf c with (NOLOCK) " +
            "where a.charge_code = b.code " +
            "  and a.serial = b.serial " +
            "  and a.charge_code = c.charge_code " +
            "  and a.serial = c.serial " +
            "  and (case " +
            "           when isnull(b.del_flag, '0') = '1' then '1' " +
            "           when isnull(b.del_flag, '0') = '1' then '1' " +
            "           when isnull(c.visible_flag_zy, '0') = '1' then '1' " +
            "           else '0' end) <> 1 " +
            "  and a.pattern_name = #{name} " +
            "  and a.dept_code = #{deptCode} ")
    List<YzOrderPatternCy> muBanXiangQing(@Param("name") String name,
                                          @Param("deptCode") String deptCode);

    @Select("select count(1) " +
            "from yz_order_pattern_cy with (NOLOCK) " +
            "where pattern_name = #{name}")
    Integer chongFuMuBanMing(String name);


    @Insert("<script>" +
            "insert into yz_order_pattern_cy (pattern_name, charge_code, serial, group_no, quantity, supply_code, instruction, " +
            "                                 dept_code, create_id, py_code, d_code) " +
            "values " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{name}, #{item.chargeCode}, #{item.serial}, #{item.groupNo}, " +
            "#{item.quantity} , null, #{item.instruction}, #{deptCode}, " +
            "        #{userCode}, #{pyCode}, #{dCode})" +
            "</foreach>" +
            "</script>")
    void chaRuMuBan(@Param("list") List<YzOrderPatternCy> list,
                    @Param("name") String name,
                    @Param("userCode") String userCode,
                    @Param("pyCode") String pyCode,
                    @Param("dCode") String dCode,
                    @Param("deptCode") String deptCode);


    @Insert("insert into yz_act_order_cy (inpatient_no, admiss_times, order_no, serial_no, order_name, order_je, " +
            "                             order_je_dj, quantity, quantity_dj, order_code_dj, doctor_code, input_id, input_date, " +
            "                             page_no, status_flag, dept_code, " +
            "                             ward_code, cy_mtjs, cy_jsl, cy_zql, cy_fj, cy_ffcs, cy_fysm, cy_jssm, " +
            "                             exec_dept, group_no, source_flag) " +
            "values (#{cy.inpatientNo}, #{cy.admissTimes}, #{cy.orderNo}, '1', #{cy.orderName}, " +
            "        cast(#{cy.orderJe,jdbcType=DECIMAL} as decimal(10,2)), " +
            "        cast(#{cy.orderJeDj,jdbcType=DECIMAL} as decimal(10,2)), #{cy.quantity}, #{cy.quantityDj}, " +
            "        (select charge_code_zydj from yz_sequence), #{cy.inputId}, #{cy.inputId}, getdate(), #{cy.pageNo}, '2', " +
            "        #{deptCode}, " +
            "        #{wardCode}, #{cy.cyMtjs}, #{cy.cyJsl}, #{cy.cyZql}, #{cy.cyFj}, " +
            "        #{cy.cyFfcs}, #{cy.cyFysm}, #{cy.cyJssm}, #{execDept}, #{cy.groupNo}, 1)")
    void chaRuCaoYaoYiZhu(@Param("cy") YzActOrderCy cy,
                          @Param("deptCode") String deptCode,
                          @Param("wardCode") String wardCode,
                          @Param("execDept") String execDept);


    @Insert("<script>" +
            "insert into yz_act_order_cy_detail (order_no, serial_no, charge_code, serial, group_no, " +
            "                                    quantity, unit_price, supply_code, instruction) " +
            "values " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{orderNo}, 1, #{item.chargeCode}, #{item.serial}, #{item.groupNo}, " +
            "        #{item.quantity}, cast(#{item.retprice,jdbcType=DECIMAL} as decimal(10,2)), null, #{item.instruction})" +
            "</foreach>" +
            "</script>")
    void chaRuCaoYaoXiangQing(@Param("list") List<YzActOrderCyDetail> list,
                              @Param("orderNo") BigDecimal orderNo);


    @Insert("insert into yz_act_order (act_order_no, inpatient_no, admiss_times, order_code , order_time," +
            " frequ_code , " +
            "                          enter_time, " +
            "                          enter_oper, start_time, infant_flag, drug_flag,  " +
            "                          physician, confirm_time, signer, status_flag, " +
            "                          self_buy, order_name, pay_self, serial, group_no, ward_code, " +
            "                          doctor_flag, dept_code, exec_unit) " +
            "values (#{yz.orderNo}, #{yz.inpatientNo}, #{yz.admissTimes}, '77592', getdate(), 'ONCE', getdate(), " +
            "        #{yz.inputId}, getdate(), #{infantFlag}, 'o', #{yz.inputId}, getdate(), #{yz.inputId}, '2', 0, " +
            "        #{orderName}, '0', '00', '00', #{wardCode}, 1, #{deptCode}, #{execDept})")
    void chaRuYiZhu(@Param("yz") YzActOrderCy cy,
                    @Param("orderName") String orderName,
                    @Param("deptCode") String deptCode,
                    @Param("wardCode") String wardCode,
                    @Param("execDept") String execDept,
                    @Param("infantFlag") Integer infantFlag);


    @Select("select top(1) * from yz_order_pattern_cy with (NOLOCK) where pattern_name = #{name}")
    YzOrderPatternCy caoYaoMuBanXiangQing(String name);

    @Delete("delete from yz_order_pattern_cy where pattern_name = #{name} ")
    void shanChuMuBan(String name);

    @Select("select * from yz_act_order_cy with (NOLOCK) " +
            "where inpatient_no = #{patNo} and admiss_times = #{times} and order_no = #{orderNo}")
    YzActOrderCy caoYaoXinXi(@Param("patNo") String patNo,
                             @Param("times") Integer times,
                             @Param("orderNo") BigDecimal orderNo);

    @Update("update yz_act_order_cy set status_flag = '${statusFlag}' " +
            "where inpatient_no = #{patNo} and admiss_times = #{times} and order_no = #{orderNo}  ")
    void genXingCaoYaoShanChuBiaoZhi(@Param("patNo") String patNo,
                                     @Param("times") Integer times,
                                     @Param("orderNo") BigDecimal orderNo,
                                     @Param("statusFlag") String statusFlag);


    @Delete("delete yz_act_order where act_order_no = #{orderNo}")
    void shanChuYiZhu(@Param("orderNo") BigDecimal orderNo);

    @Delete("delete yz_act_order where act_order_no = #{orderNo};" +
            "delete yz_act_order_cy where inpatient_no = '${patNo}' and admiss_times = ${times} and order_no = #{orderNo};" +
            "delete yz_act_order_cy_detail where order_no = #{orderNo};")
    void manualTransactions(@Param("patNo") String patNo,
                            @Param("times") Integer times,
                            @Param("orderNo") BigDecimal orderNo);
}
