package thyyxxk.webserver.dao.his.reports;

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.reports.medicinestockcount.GetStockParam;
import thyyxxk.webserver.entity.reports.medicinestockcount.StockDetail;
import thyyxxk.webserver.entity.reports.medicinestockcount.StockIndex;
import thyyxxk.webserver.entity.reports.medicinestockcount.StockSuggestion;

import java.util.List;

@Mapper
public interface MedicineStockCountDao {
    @Select("select distinct " +
            "draw_no, " +
            "out_date, " +
            "confirm_flag = isnull(confirm_flag,'0') " +
            "from " +
            "yp_out_detl_yf " +
            "where out_type>='6'and " +
            "out_type<='8'and " +
            "datediff(dd,out_date, #{start})<=0 and " +
            "datediff(dd,out_date, #{end})>=0 and " +
            "group_no ='73' order by out_date")
    List<StockIndex> getStockIndex(GetStockParam param);

    @Select("SELECT DISTINCT a.draw_no, " +
            "a.group_no, " +
            "inYf=(select sum(buy_amt) from yp_in_detl_yf " +
            "where in_type in (0, 1, 3) and charge_code=a.charge_code and group_no=73 " +
            "and serial=a.serial and in_date>=#{start} and in_date<=#{end})," +
            "a.out_date, " +
            "c.location, " +
            "a.charge_code, " +
            "a.serial, " +
            "b.name, " +
            "b.specification, " +
            "a.pack_retprice, " +
            "a.stock_amount, " +
            "curr_stock_amount=a.stock_amount-a.out_amt, " +
            "amount_pd=-a.out_amt, " +
            "charge_pd=-a.out_amt*a.pack_retprice, " +
            "a.confirm_date, " +
            "confirm_flag=isnull(confirm_flag,'0'), " +
            "a.confirm_id, " +
            "a.input_id, " +
            "b.categories_flag, " +
            "buy_price = case when isnull(a.buy_price,0)=0 then b.buy_price else a.buy_price end, " +
            "grje=(a.stock_amount-a.out_amt)* (case when isnull(a.buy_price,0)=0 then b.buy_price else a.buy_price end), " +
            "lsje=(a.stock_amount-a.out_amt)*a.pack_retprice, " +
            "sort_no =left(c.location,1), " +
            "b.drug_kind, " +
            "manu_code = b.manu_code " +
            "FROM yp_out_detl_yf a, " +
            "yp_zd_dict b, " +
            "yp_base_yf c " +
            "WHERE ( a.charge_code = b.code ) and   " +
            "( a.serial = b.serial ) and " +
            "( a.charge_code = c.charge_code ) and   " +
            "( a.serial = c.serial ) and " +
            "( a.group_no = c.group_no ) and   " +
            "( ( a.draw_no = #{drawNo} ) AND " +
            "( out_type > =6 ) AND   " +
            "( out_type < =8 ) )  and  " +
            " case when 0=1 then 0 else 9999999 end  <>a.out_amt " +
            "ORDER BY b.drug_kind ASC, " +
            "c.location ASC")
    List<StockDetail> getStockDetail(GetStockParam param);

    @Select("SELECT DISTINCT a.draw_no, " +
            "a.group_no, " +
            "a.out_date, " +
            "c.location, " +
            "a.charge_code, " +
            "a.serial, " +
            "b.name, " +
            "b.specification, " +
            "a.pack_retprice, " +
            "a.stock_amount, " +
            "curr_stock_amount=a.stock_amount-a.out_amt, " +
            "amount_pd=-a.out_amt, " +
            "charge_pd=-a.out_amt*a.pack_retprice, " +
            "a.confirm_date, " +
            "confirm_flag=isnull(confirm_flag,'0'), " +
            "a.confirm_id, " +
            "a.input_id, " +
            "b.categories_flag, " +
            "buy_price = case when isnull(a.buy_price,0)=0 then b.buy_price else a.buy_price end, " +
            "grje=(a.stock_amount-a.out_amt)* (case when isnull(a.buy_price,0)=0 then b.buy_price else a.buy_price end), " +
            "lsje=(a.stock_amount-a.out_amt)*a.pack_retprice, " +
            "sort_no =left(c.location,1), " +
            "b.drug_kind, " +
            "manu_code = b.manu_code " +
            "FROM yp_out_detl_yf a, " +
            "yp_zd_dict b, " +
            "yp_base_yf c " +
            "WHERE ( a.charge_code = b.code ) and   " +
            "( a.serial = b.serial ) and " +
            "( a.charge_code = c.charge_code ) and   " +
            "( a.serial = c.serial ) and " +
            "( a.group_no = c.group_no ) and   " +
            "( ( a.draw_no = #{drawNo} ) AND " +
            "( out_type > =6 ) AND   " +
            "( out_type < =8 ) )  and  " +
            " case when 0=1 then 0 else 9999999 end  <>a.out_amt " +
            "ORDER BY b.drug_kind ASC, " +
            "c.location ASC")
    List<StockDetail> getLastStockDetail(@Param("drawNo") String drawNo);

    @Select("select aa.charge_code_mx, aa.serial, aa.name, " +
            "aa.amount1,isnull(bb.amount2,0) amount2,isnull(cc.amount3,0) amount3,dd.amount4, " +
            "(aa.amount1+isnull(bb.amount2,0)+isnull(cc.amount3,0)) zy_total, " +
            "(dd.amount4-aa.amount1-bb.amount2) diff1, " +
            "dd.amount4-(aa.amount1+isnull(bb.amount2,0)+isnull(cc.amount3,0)) diff2 " +
            "from " +
            "(select charge_code_mx, a.serial, b.name,sum(a.charge_amount) amount1,sum(a.charge_fee) fee1 " +
            "from zy_detail_charge a,yp_zd_dict b " +
            "where charge_date>=#{start} " +
            "and charge_date<=#{end} " +
            "and LEN(order_no)>2 " +
            "and a.charge_code_mx=b.code " +
            "and a.serial=b.serial " +
            "group by a.charge_code_mx, a.serial, b.name) aa " +
            "left join " +
            "(select charge_code_mx, a.serial, b.name,sum(a.charge_amount) amount2,sum(a.charge_fee) fee2 " +
            "from zy_detail_charge a,yp_zd_dict b " +
            "where charge_date>=#{start} " +
            "and charge_date<=#{end} " +
            "and order_no=0 " +
            "and a.charge_code_mx=b.code " +
            "and a.serial=b.serial " +
            "group by a.charge_code_mx, a.serial, b.name) bb on aa.charge_code_mx=bb.charge_code_mx " +
            "and aa.serial=bb.serial " +
            "left join " +
            "(select charge_code_mx, a.serial, b.name,sum(a.charge_amount) amount3,sum(a.charge_fee) fee3 " +
            "from zy_detail_charge a,yp_zd_dict b " +
            "where charge_date>=#{start} " +
            "and charge_date<=#{end} " +
            "and order_no=6 " +
            "and a.charge_code_mx=b.code " +
            "and a.serial=b.serial " +
            "group by a.charge_code_mx, a.serial, b.name) cc on aa.charge_code_mx=cc.charge_code_mx " +
            "and aa.serial=cc.serial " +
            "left join " +
            "(SELECT a.charge_code, a.serial, b.name,sum(a.amount) amount4 " +
            "FROM yp_zy_patient a,yp_zd_dict b " +
            "where a.charge_code=b.code and " +
            "a.serial =b.serial and " +
            "a.group_no in ('71', '73') and a.order_date >=#{start} and a.order_date <=#{end} " +
            "group by a.charge_code, a.serial, b.name) dd on aa.charge_code_mx=dd.charge_code and " +
            "aa.serial = dd.serial")
    List<StockSuggestion> getStockSuggestion(GetStockParam param);

    @Update("update yp_out_detl_yf set stock_amount=#{stockAmount}, out_amt=#{outAmt} " +
            "where draw_no=#{drawNo} and charge_code=#{chargeCode} and serial=#{serial}")
    void updateStockChanges(StockDetail param);

    @Update("update yp_out_detl_yf set stock_amount=#{lastCurrStockAmount} " +
            "where draw_no=#{lastDrawNo} and charge_code=#{chargeCode} and serial=#{serial}")
    void updateLastStockAmount(StockDetail param);
}
