package thyyxxk.webserver.dao.his.datamodify;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
import thyyxxk.webserver.entity.datamodify.InOrOutOfWarehousePojo;
import thyyxxk.webserver.entity.datamodify.YpInDetl;
import thyyxxk.webserver.entity.datamodify.YpOutDetl;

import java.util.List;

/**
 * <p>
 * 描述： 药品入库或出库
 * </p>
 *
 * @author xc
 * @date 2021-06-01 14:06
 */
public interface InOrOutOfWarehouseDao {

    /**
     * 根据入库单号 或者 入库时间 来查询数据
     *
     * @param page  分页对象
     * @param param currentPage pageSize startTime endTime inDocuNo：入库单号
     * @return 返回分页的数据
     */
    @Select("<script>" +
            "select supply_code_name = (select name from yp_zd_supply with(nolock) where code = supply_code), " +
            "manu_code_name = (select name from yp_zd_manufactory with(nolock) where code = manu_code)," +
            "purchaser_name = (select  name from a_employee_mi with(nolock) where code = purchaser)," +
            "charge_code_name = (select name from yp_zd_dict with(nolock) where " +
            "serial = yp_in_detl.serial and code = charge_code),* " +
            "from yp_in_detl with(nolock) where 1=1 " +
            "<if test=\"param.startTime!=null and param.startTime!=''\">" +
            "and in_date &gt;= #{param.startTime} and in_date&lt;=#{param.endTime} " +
            "</if>" +
            "<if test=\"param.inDocuNo!=null and param.inDocuNo!=''\">" +
            "and in_docu_no=#{param.inDocuNo}" +
            "</if>" +
            "</script>")
    List<YpInDetl> queryRuKuXinXi(IPage<InOrOutOfWarehousePojo> page,
                                  @Param("param") InOrOutOfWarehousePojo param);

    /**
     * 根据不同拼音码 和 不同的表格来查询数据
     *
     * @param tableName 要查询的表格名称
     * @param pyCode    拼音码
     * @return 返回多个 code 和 name
     */
    @Select("<script>" +
            "select code,rtrim(name)name from ${tableName} with(nolock) where py_code like #{pyCode} and isnull(del_flag,0)=0" +
            "<if test=\"tableName == 'a_employee_mi'\">" +
            "and dept_code = '2060000' " +
            "</if>" +
            "</script>")
    List<GetDropdownBox> queryCode(@Param("tableName") String tableName,
                                   @Param("pyCode") String pyCode);

    /**
     * 更新 入库信息
     *
     * @param param 要更新的字段 以及条件
     */
    @Update("update yp_in_detl set in_docu_no=#{inDocuNo},supply_code=#{supplyCode},rept_no=#{reptNo}, " +
            "purchaser=#{purchaser},fp_no=#{fpNo},manu_no=#{manuNo},manu_code=#{manuCode}," +
            "license_no=#{licenseNo},in_comment=#{inComment}, " +
            "in_date=#{inDate},sys_date=#{inDate},confirm_date=#{confirmDate}," +
            "fp_date=#{fpDate},eff_date=#{effDate} " +
            "where in_docu_no = #{oldInDocuNo} and charge_code = #{chargeCode} and in_seri=#{inSeri}")
    void xiuGiRuKuXinXi(YpInDetl param);

    /**
     * 根据入库单号 或者 入库时间 来查询数据
     *
     * @param page  分页对象
     * @param param currentPage pageSize startTime endTime drawNo：出库单号
     * @return 返回分页的数据
     */
    @Select("<script>" +
            "select charge_code,out_seri,out_seri old_out_seri,draw_no,draw_no old_draw_no,in_draw_no,in_date,group_no,group_no_out,dept_code,out_date,confirm_date," +
            "out_amt,manu_no,  " +
            "dept_code_name = (select name from zd_unit_code with(nolock) where code = dept_code),  " +
            "group_no_name = (select group_name from yp_zd_group_name with(nolock) where group_no = yp_out_detl.group_no) ," +
            "group_no_out_name = (select group_name from yp_zd_group_name with(nolock) where group_no = yp_out_detl.group_no) ," +
            "charge_code_name = (select name from yp_zd_dict with(nolock) where  " +
            "code = charge_code and serial = yp_out_detl.serial)  " +
            "from yp_out_detl with(nolock) where 1=1 " +
            "<if test=\"param.startTime!=null and param.startTime!=''\">" +
            "and out_date &gt;= #{param.startTime} and out_date&lt;=#{param.endTime} " +
            "</if>" +
            "<if test=\"param.drawNo!=null and param.drawNo!=''\">" +
            "and draw_no=#{param.drawNo} " +
            "</if>" +
            "<if test=\"param.inDrawNo!=null and param.inDrawNo!=''\">" +
            "and in_draw_no=#{param.inDrawNo} " +
            "</if>" +
            "<if test=\"param.chargeCode!=null and param.chargeCode!=''\">" +
            "and charge_code=#{param.chargeCode} " +
            "</if>" +
            "</script>")
    List<YpOutDetl> queryChuKuXinXi(IPage<InOrOutOfWarehousePojo> page, @Param("param") InOrOutOfWarehousePojo param);

    /**
     * 获取到科室的code
     *
     * @return 返回全部的科室 code name
     */
    @Select("select code,rtrim(name)name from zd_unit_code with(nolock) where isnull(del_flag,0)=0")
    List<GetDropdownBox> queryDeptCode();

    /**
     * 获取 药房的 code name
     *
     * @return 返回code name
     */
    @Select("select group_no code,rtrim(group_name) name from yp_zd_group_name with(nolock) ")
    List<GetDropdownBox> queryGroupNo();

    /**
     * 更新 出库的信息
     *
     * @param param 要更新的字段
     */
    @Update("UPDATE yp_out_detl SET draw_no=#{drawNo}, " +
            "out_seri=#{outSeri}, " +
            "in_draw_no=#{inDrawNo}, " +
            "in_date=#{inDate}, " +
            "group_no=#{groupNo}, " +
            "out_amt=#{outAmt}, " +
            "manu_no=#{manuNo}, " +
            "group_no_out=#{groupNoOut}, " +
            "dept_code=#{deptCode}, " +
            "out_date=#{outDate}, " +
            "sys_date=#{outDate}, " +
            "confirm_date=#{confirmDate} " +
            "WHERE out_seri=#{oldOutSeri} " +
            "AND draw_no=#{oldDrawNo} " +
            "AND charge_code = #{chargeCode}")
    void xiuGaiChuKu(YpOutDetl param);

    /**
     * 在修改出库信息的时候要同步修改 药库的信息
     *
     * @param param 修改的参数 以及 查询到的条件
     */
    @Update("update yp_in_detl_yf set " +
            "manu_no=#{manuNo},buy_amt = #{outAmt},group_no=#{groupNoOut} " +
            "where charge_code = #{chargeCode} and out_draw_no = #{oldDrawNo} and in_seri = #{oldOutSeri}")
    void xiuGaiYaoFangRuKuXinXi(YpOutDetl param);

    /**
     * 删除出库的一条记录
     *
     * @param drawNo     出库号
     * @param chargeCode 药品编码
     * @param outSeri    序号
     * @param inDrawNo   入库单号
     */
    @Delete("<script>" +
            "delete from yp_out_detl with(nolock) where draw_no=#{drawNo} and charge_code=#{chargeCode} and out_seri=#{outSeri} " +
            "<if test=\"inDrawNo!=null and inDrawNo!=''\">" +
            "and in_draw_no=#{inDrawNo}" +
            "</if> " +
            "</script>")
    Integer shanChuChuKuXinXin(@Param("drawNo") String drawNo,
                               @Param("chargeCode") String chargeCode,
                               @Param("outSeri") Integer outSeri,
                               @Param("inDrawNo") String inDrawNo);

    /**
     * 在删除出库信息的时候也要同步删除 药库的信息
     *
     * @param outDrawNo  出库单号
     * @param chargeCode 药品编码
     * @param inSeri     序号
     * @return 返回收到影响的行数 用来判断是否真的删除成功了
     */
    @Delete("delete from yp_in_detl_yf with(nolock) where out_draw_no=#{outDrawNo} and charge_code=#{chargeCode} and in_seri=#{inSeri}")
    Integer shanChuYaoFangShuJu(@Param("outDrawNo") String outDrawNo,
                                @Param("chargeCode") String chargeCode,
                                @Param("inSeri") Integer inSeri);
}
