package thyyxxk.webserver.dao.his.medicine;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.medicine.YpPlanDetail;

import java.util.List;

@Mapper
public interface YpPlanDetailDao extends BaseMapper<YpPlanDetail> {

    @Select(" select * from yp_plan_detail ")
    List<YpPlanDetail> selectYpPlanDetail(YpPlanDetail detail);

    @Select(" select * from yp_plan_detail where file_id = #{fileId} ")
    List<YpPlanDetail> selectYpPlanDetailById(@Param("fileId") String fileId);

    @Delete(" delete from yp_plan_detail where file_id = #{fileId} ")
    int deleteYpPlanDetailById(@Param("fileId") String fileId);

    @Insert({"<script>" +
            " insert into yp_plan_detail (file_id, remark, drug_name, specification, pack_retprice, buy_price, amount, manu_name, " +
            " amount3, amount7, amount14, amount30, amount60, days, buy_amount, unit, buy_fee, content, imp_date, supply_name, " +
            " buy_money, pack_retprice1, buy_price1, charge_code, drug_kind, is_winning, month, plan_total, real_total) values " +
            "<foreach collection='list' item='item' separator=','>" +
            " (#{item.fileId}, #{item.remark}, #{item.drugName}, #{item.specification}, #{item.packRetprice}, #{item.buyPrice}, #{item.amount}, #{item.manuName}, " +
            " #{item.amount3}, #{item.amount7}, #{item.amount14}, #{item.amount30}, #{item.amount60}, #{item.days}, #{item.buyAmount}, #{item.unit}, #{item.buyFee}, " +
            " #{item.content}, #{item.impDate}, #{item.supplyName}, #{item.buyMoney}, #{item.packRetprice1}, #{item.buyPrice1}, " +
            " #{item.chargeCode}, #{item.drugKind}, #{item.isWinning}, #{item.month}, #{item.planTotal}, #{item.realTotal})" +
            "</foreach>" +
            "</script>"})
    int insertList(List<YpPlanDetail> list);

    @Select(" select isnull(sum(a.buy_amount), 0) as sl from yp_plan_detail a, yp_plan_info b " +
            " where a.file_id = b.file_id and a.charge_code = #{code} and a.month = #{ny} " +
            " and b.final_audit = '1' ")
    int selectYpPlanTotalByMonth(@Param("code") String code, @Param("ny") String ny);

    @Select(" select isnull(sum(buy_amt), 0) as sl from yp_in_detl " +
            " where in_type = '0' and confirm_flag = '1' and charge_code = #{code} and convert(varchar(6), in_date, 112) = #{ny}  ")
    int selectYpRealTotalByMonth(@Param("code") String code, @Param("ny") String ny);

}
