package thyyxxk.webserver.dao.his.zhuyuanyiji;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.medicaltechnology.AsidePage;
import thyyxxk.webserver.entity.medicaltechnology.FetchMedicines;
import thyyxxk.webserver.entity.medicaltechnology.MedicinePage;
import thyyxxk.webserver.entity.medicaltechnology.PoisonousAnesthetics;

import java.util.List;

@Mapper
public interface MedicineManagementDao {

    @Select("select rtrim(code) from zd_unit_code where parent_code=#{code} ")
    List<String> selectChildDepartments(String code);

    @Select("select b.code as chargeCode,sum(a.amount) as amount,sum(a.charge_fee) as chargeFee, " +
            "max(b.pack_retprice) as price,max(a.exec_unit) as execUnit,b.serial as serialNo, " +
            "max(a.group_no) as groupNo,max(a.page_no) as pageNo,max(b.name) as drugName, " +
            "max(b.specification) as spec,max(a.confirm_flag) as confirmFlag,max(b.drug_flag) as drugFlag " +
            "from zy_drug a with(nolock),yp_zd_dict b with(nolock) ${ew.customSqlSegment}")
    List<MedicinePage> selectMedicinePages(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select b.code as chargeCode,rtrim(c.name) as patName,rtrim(c.inpatient_no) as patNo,sum(a.amount) as amount, " +
            "sum(a.charge_fee) as chargeFee,max(b.pack_retprice) as price,max(a.exec_unit) as execUnit,a.charge_date, " +
            "b.serial as serialNo,max(a.group_no) as groupNo,max(a.page_no) as pageNo,max(b.name) as drugName, " +
            "max(b.specification) as spec,max(a.confirm_flag) as confirmFlag,max(b.drug_flag) as drugFlag " +
            "from zy_drug a with(nolock),yp_zd_dict b with(nolock),zy_actpatient c with(nolock) " +
            "where charge_date>=#{start} and charge_date<=#{end} and a.group_no='73' and a.exec_unit in (${dept}) " +
            "and a.charge_code=b.code and a.inpatient_no=c.inpatient_no and isnull(a.serial,'01')=b.serial " +
            "group by b.code,b.serial,c.inpatient_no,c.name,a.charge_date having sum(a.amount)!=0")
    List<MedicinePage> selectUsedMedicines(FetchMedicines params);

    @Delete("delete yz_yp_page_no where page_no=#{pageNo}")
    void deleteExistTargetPageNo(int pageNo);

    @Insert("insert into yz_yp_page_no (page_no,dept_code,ward_code,submit_time,submit_name,page_class,page_name," +
            "start_date,end_date,group_no,submit_flag) values (#{pageNo},#{dept},#{dept},getdate(),'00000','A'," +
            "'医技科室药品单',#{start},#{end},'73','1' )")
    void insertNewPageNo(FetchMedicines params);

    @Update("update zy_drug set page_no=#{pageNo},sub_date=getdate() where charge_date>=#{start} " +
            "and charge_date<=#{end} and isnull(page_no,0)=0 and exec_unit=#{dept} " +
            "and zy_drug.charge_code in (${chargeCodes}) ")
    void submitMedicinePages(FetchMedicines params);

    @Select("select convert(varchar(5),a.gen_time,10) as chargeDate,b.name as chargeName,c.name as deptName,rtrim(d.name) as patName, " +
            "patGender=case when isnull(d.sex,'')='' then '' when d.sex='1'then '男'else '女'end, " +
            "d.social_no,rtrim(a.inpatient_no) as patNo,rtrim(e.admiss_diag_str) as admdiag, " +
            "b.specification2 as spec,a.charge_amount as chargeAmount,a.charge_fee as chargeFee,rtrim(a.charge_code_mx) as chargeCode " +
            "from zy_detail_charge a left join a_patient_mi d on a.inpatient_no=d.inpatient_no " +
            "left join zy_actpatient e on a.inpatient_no=e.inpatient_no,zd_unit_code c, " +
            "(select name,specification2,code,drug_flag from yp_zd_dict where drug_flag>0 group by code,name,specification2,drug_flag) b " +
            "where a.gen_time>=#{start} and a.gen_time<=#{end} and a.exec_unit=#{dept} and a.ledger_sn>0 " +
            "and a.charge_code_mx=b.code and a.exec_unit=c.code " +
            "union all " +
            "select convert(varchar(5),a.charge_date,10) as chargeDate,b.name as chargeName,'麻醉科' as deptName,rtrim(d.name) as patName, " +
            "patGender=case when isnull(d.sex,'')='' then '' when d.sex='1'then '男'else '女'end, " +
            "d.social_no,rtrim(a.patient_id) as patNo,admdiag='', " +
            "b.specification2 as spec,a.quantity as chargeAmount,a.unit_price*quantity as chargeFee, " +
            "rtrim(a.charge_item_code) as chargeCode " +
            "from mz_charge_detail_b a left join mz_patient_mi d on a.patient_id=d.patient_id, " +
            "(select name,specification2,code,drug_flag from yp_zd_dict where drug_flag>0 group by code,name,specification2,drug_flag) b " +
            "where a.charge_date>=#{start} and a.charge_date<=#{end} and a.warn_dept!='1070000' and a.charge_item_code=b.code")
    List<PoisonousAnesthetics> selectPoisonousAnesthetics(FetchMedicines params);

    @Select("select page_no,confirmDate=max(confirm_date),confirmId=max(confirm_id),exec_unit, " +
            "confirmFlag=max(isnull(confirm_flag_new,0)) from zy_drug ${ew.customSqlSegment} " )
    List<AsidePage> selectSubmittedPageNos(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);

    @Select("select chargeCode=b.code,amount=sum(a.amount),chargeFee=sum(a.charge_fee), " +
            "amountFy=sum(case when confirm_flag='1' then a.amount else 0 end), " +
            "price=max(a.retprice),pageNo=max(a.page_no),drugName=max(b.name), " +
            "spec=max(b.specification),confirmFlag=max(a.confirm_flag_new), " +
            "confirmDate=max(confirm_date) from zy_drug a,yp_zd_dict b ${ew.customSqlSegment}")
    List<MedicinePage> fetchMedicinePageDetail(@Param(Constants.WRAPPER) QueryWrapper<?> queryWrapper);
}

