package thyyxxk.webserver.dao.his.orderZdMaintain;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.controller.orderZdMaintain.OrderZdMaintainController;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.dictionary.YzOrderOccurence;
import thyyxxk.webserver.entity.dictionary.ZdChargeClass;
import thyyxxk.webserver.entity.executeItem.ZdChargeItem;
import thyyxxk.webserver.entity.orderZdMaintain.dto.*;
import thyyxxk.webserver.entity.orderZdMaintain.dto.frequency.YzOrderFrequencyDTO;
import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.YzZdOrderItemConfirm;

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

@Mapper
public interface OrderZdMaintainDao {

    @Select("SELECT rtrim(code) code, rtrim(name) name\n" +
            "FROM zd_charge_item_type")
    List<CodeName> getChangeItemType();

    @Select("SELECT order_class,\n" +
            "       yz_order_item.order_code,\n" +
            "       order_class_name = (select name from zd_charge_item_type where code = order_class),\n" +
            "       rtrim(order_name) order_name,\n" +
            "       yz_order_item.py_code,\n" +
            "       yz_order_item.d_code,\n" +
            "       yz_order_item.exclu_group_type,\n" +
            "       del_flag         =isnull(yz_order_item.del_flag, '0'),\n" +
            "       yz_order_item.discription,\n" +
            "       yz_order_item.operation,\n" +
            "       yz_order_item.owe_occ,\n" +
            "       yz_order_item.dl_code,\n" +
            "       yz_order_item.xl_code,\n" +
            "       yz_order_item.hl_class,\n" +
            "       comment1,\n" +
            "       discription,\n" +
            "       n_code,\n" +
            "       new_flag='0',\n" +
            "       order_type,\n" +
            "       order_type_name  = (SELECT name from jc_zd_class where code = order_type),\n" +
            "       yp_flag,\n" +
            "       jy_type,\n" +
            "       jy_type_name     = (select name from jy_zd_class a where a.code = jy_type),\n" +
            "       yz_order_item.yb_comment,\n" +
            "       ss_type,\n" +
            "       frequ_type,\n" +
            "       exec_dept,\n" +
            "       min_age_restriction,\n" +
            "       max_age_restriction,\n" +
            "       gender_restriction,\n" +
            "       execDeptName=(select rtrim(name) from zd_unit_code b where exec_dept = b.code),\n" +
            "       qj_flag\n" +
            "FROM yz_order_item where ${where}")
    List<YzOrderItemDTO> getOrderItem(String where);


    @Select("SELECT a.occ_code,\n" +
            "       a.order_code,\n" +
            "       a.amount  ,\n" +
            "       name=b.name,\n" +
            "       b.e_code,\n" +
            "       price=b.charge_amount\n" +
            "FROM yz_order_occurence a  ,zd_charge_item b\n" +
            "where    a.order_code=#{orderCode} and\n" +
            "    a.occ_code=b.code")
    List<OrderOccurrenceDTO> getChangeItem(String orderCode);

    @Select("SELECT zd_charge_item.code,\n" +
            "       zd_charge_item.exec_unit,\n" +
            "       zd_charge_item.name,\n" +
            "       price=zd_charge_item.charge_amount,\n" +
            "       zd_charge_item.bill_item_code,\n" +
            "       zd_charge_item.py_code,\n" +
            "       zd_charge_item.d_code,\n" +
            "       zd_charge_item.suppress_flag,\n" +
            "       zd_charge_item.bill_item_out,\n" +
            "       zd_charge_item.n_code,\n" +
            "       zd_charge_item.self_flag,\n" +
            "       zd_charge_item.separate_flag,\n" +
            "       zd_charge_item.e_code,\n" +
            "       spec=charge_unit,\n" +
            "       exec_name=(select rtrim(name) from zd_unit_code b where exec_unit = b.code),\n" +
            "       serial='00',\n" +
            "       group_no='00',\n" +
            "       del_flag\n" +
            "FROM zd_charge_item\n" +
            "where  (name like '%${code}%' or py_code like '%${code}%' or code like '%${code}%') and del_flag <> '1' " +
            "order by zd_charge_item.name")
    List<ZdChargeItemDto> searchChangeItem(String code);


    @Select("SELECT code,\n" +
            "       name\n" +
            "FROM ysh_zd_ss_type\n" +
            "where isnull(del_flag, '0') <> '1'")
    List<CodeName> getSsType();

    @Update("update yz_order_item\n" +
            "set order_name       = #{params.orderName},\n" +
            "    order_class      = #{params.orderClass},\n" +
            "    operation        = #{params.operation},\n" +
            "    del_flag         = #{params.delFlag},\n" +
            "    exclu_group_type = #{params.excluGroupType},\n" +
            "    qj_flag          = #{params.qjFlag},\n" +
            "    frequ_type       = #{params.frequType},\n" +
            "    yp_flag          = #{params.ypFlag},\n" +
            "    jy_type          = null,\n" +
            "    exec_dept        = #{params.execDept},\n" +
            "    ss_type          = #{params.ssType},\n" +
            "    discription      = #{params.discription},\n" +
            "    yb_comment       = #{params.ybComment},\n" +
            "    min_age_restriction       = #{params.minAgeRestriction},\n" +
            "    max_age_restriction       = #{params.maxAgeRestriction},\n" +
            "    gender_restriction       = #{params.genderRestriction},\n" +
            "    n_code           = #{params.nCode},\n" +
            "    py_code = #{params.pyCode}," +
            "    d_code = #{params.dCode} " +
            "where order_code = #{params.orderCode}")
    void updateChangeItem(@Param("params") YzOrderItemDTO params);


    @Insert("insert into yz_order_item (order_code, order_name, order_class, operation, del_flag, exclu_group_type, qj_flag,\n" +
            "                           frequ_type, yp_flag,\n" +
            "                           jy_type, exec_dept, ss_type, discription, yb_comment, n_code,py_code,d_code,min_age_restriction,max_age_restriction,gender_restriction)\n" +
            "values (#{params.orderCode}, #{params.orderName}, #{params.orderClass}, #{params.operation}, #{params.delFlag},\n" +
            "        #{params.excluGroupType}, #{params.qjFlag}, #{params.frequType}, #{params.ypFlag}, null,\n" +
            "        #{params.execDept}, #{params.ssType}, #{params.discription}, #{params.ybComment}, #{params.nCode}, #{params.pyCode}, #{params.dCode}, #{params.minAgeRestriction}, #{params.maxAgeRestriction}, #{params.genderRestriction})")
    int insertOrderItem(@Param("params") YzOrderItemDTO params);


    @Update("update yz_order_item set del_flag = #{delFlag} where order_code = #{orderCode}")
    void stopChangeItem(@Param("orderCode") String orderCode,
                        @Param("delFlag") String delFlag);


    @Delete("delete yz_order_occurence where order_code = #{orderCode}")
    void delOcc(String orderCode);

    @Insert("INSERT INTO yz_order_occurence ( occ_code, order_code, amount ) " +
            "VALUES ( #{params.occCode}, #{params.orderCode}, #{params.amount} )\n")
    void insertOcc(@Param("params") AddOrderOccurenceDTO.OrderItem params);

    @Select("select * from yz_zd_order_item_confirm")
    List<YzZdOrderItemConfirm> getAllYzOrderItemConfirm();


    @Insert("insert into yz_zd_order_item_confirm(item_name, order_code, count) values (#{param.itemName},#{param.orderCode},#{param.count})")
    int insertOrderItemConfirm(@Param("param") YzZdOrderItemConfirm param);


    @Delete("delete  yz_zd_order_item_confirm where order_code = #{param.orderCode} and item_name = #{param.itemName}")
    void deleteOrderItemConfirm(@Param("param") YzZdOrderItemConfirm param);

    @Select("SELECT rtrim(code) as code,\n" +
            "       rtrim(comm) as comm,\n" +
            "       week_day,\n" +
            "       day_600,\n" +
            "       day_630,\n" +
            "       day_700,\n" +
            "       day_730,\n" +
            "       day_800,\n" +
            "       day_830,\n" +
            "       day_900,\n" +
            "       day_930,\n" +
            "       day_1000,\n" +
            "       day_1030,\n" +
            "       day_1100,\n" +
            "       day_1130,\n" +
            "       day_1200,\n" +
            "       day_1230,\n" +
            "       day_1300,\n" +
            "       day_1330,\n" +
            "       day_1400,\n" +
            "       day_1430,\n" +
            "       day_1500,\n" +
            "       day_1530,\n" +
            "       day_1600,\n" +
            "       day_1630,\n" +
            "       day_1700,\n" +
            "       day_1730,\n" +
            "       day_1800,\n" +
            "       day_1830,\n" +
            "       day_1900,\n" +
            "       day_1930,\n" +
            "       day_2000,\n" +
            "       day_2030,\n" +
            "       day_2100,\n" +
            "       day_2130,\n" +
            "       day_2200,\n" +
            "       day_2230,\n" +
            "       day_2300,\n" +
            "       day_2330,\n" +
            "       day_2400,\n" +
            "       day_030,\n" +
            "       day_100,\n" +
            "       day_130,\n" +
            "       day_200,\n" +
            "       day_230,\n" +
            "       day_300,\n" +
            "       day_330,\n" +
            "       day_400,\n" +
            "       day_430,\n" +
            "       day_500,\n" +
            "       day_530,\n" +
            "       week_1,\n" +
            "       week_2,\n" +
            "       week_3,\n" +
            "       week_4,\n" +
            "       week_5,\n" +
            "       week_6,\n" +
            "       week_7,\n" +
            "       donot_modify,\n" +
            "       rtrim(print_name) as print_name,\n" +
            "       sort_code,\n" +
            "       special_flag,\n" +
            "       del_flag,\n" +
            "       new_flag='0',\n" +
            "       self_buy,\n" +
            "       use_time\n" +
            "FROM yz_order_frequency")
    List<YzOrderFrequencyDTO> selectOrderFrequency();

    @Select("select count(1) from yz_order_frequency where code = #{code}")
    int hasOrderFrequency(String code);


    @Update("update yz_order_frequency set del_flag = #{delFlag} where code = #{code}")
    void deleteOrderFrequency(String code, String delFlag);


    @Select("SELECT                rtrim(supply_name) as supply_name,\n" +
            "                      rtrim(supply_code) as supply_code,\n" +
            "                      rtrim(supply_occ)  as supply_occ,\n" +
            "                      del_flag,\n" +
            "                      sort_code,\n" +
            "                      class              as classs,\n" +
            "                      rtrim(print_name) AS print_name,\n" +
            "                      rtrim(py_code) AS py_code,\n" +
            "                      rtrim(d_code) AS  d_code,\n" +
            "                      supply_child,\n" +
            "                      self_buy,\n" +
            "                      ps_flag,\n" +
            "                      pring_flag,\n" +
            "    supplyChildName = (select rtrim(c.supply_name) from yz_supply_type c where c.supply_code = a.supply_child)\n" +
            "FROM yz_supply_type a order by supply_code ")
    List<YzSupplyTypeDTO> selectAllSupplyType();

    @Select("SELECT                \n" +
            "rtrim(supply_name) as supply_name,\n" +
            "rtrim(supply_code) as supply_code,\n" +
            "rtrim(supply_occ)  as supply_occ,\n" +
            "del_flag,sort_code,class_code,\n" +
            "rtrim(print_name) AS print_name,\n" +
            "rtrim(py_code) AS py_code,\n" +
            "rtrim(d_code) AS  d_code,\n" +
            "child_code,try_flag,\n" +
            "childName = (select rtrim(c.supply_name) from mz_zd_supply_type c where c.supply_code = a.child_code)\n" +
            "FROM mz_zd_supply_type a order by supply_code ")
    List<MzZdSupplyType> selectAllMzSupplyType();

    @Insert("insert into yz_supply_type (supply_code, supply_name, del_flag, class, print_name, py_code,\n" +
            "                            d_code, supply_child, self_buy, ps_flag, pring_flag)\n" +
            "values (#{supplyCode}, #{supplyName}, '0', #{classs}, #{printName}, #{pyCode}, #{dCode}, #{supplyChild}, #{selfBuy},\n" +
            "        #{psFlag}, #{pringFlag})")
    void insertSupplyType(YzSupplyTypeDTO yzSupplyTypeDTO);

    @Insert("insert into mz_zd_supply_type (supply_code, supply_name, del_flag, class_code, " +
            "print_name, py_code,d_code, child_code, try_flag) values " +
            "(#{supplyCode}, #{supplyName}, '0', #{classCode}, #{printName}, #{pyCode}, " +
            "#{dCode}, #{childCode}, #{tryFlag})")
    void insertMzSupplyType(MzZdSupplyType mzZdSupplyType);

    @Update("UPDATE yz_supply_type \n" +
            "    SET \n" +
            "        supply_name = #{supplyName},\n" +
            "        del_flag = #{delFlag},\n" +
            "        class = #{classs},\n" +
            "        print_name = #{printName},\n" +
            "        py_code = #{pyCode},\n" +
            "        d_code = #{dCode},\n" +
            "        supply_child = #{supplyChild},\n" +
            "        self_buy = #{selfBuy},\n" +
            "        ps_flag = #{psFlag},\n" +
            "        pring_flag = #{pringFlag}\n" +
            "    WHERE \n" +
            "        supply_code = #{supplyCode}")
    void updateSupplyType(YzSupplyTypeDTO yzSupplyTypeDTO);

    @Update("UPDATE mz_zd_supply_type SET \n" +
            "supply_name = #{supplyName},\n" +
            "del_flag = #{delFlag},\n" +
            "class_code = #{classCode},\n" +
            "print_name = #{printName},\n" +
            "py_code = #{pyCode},\n" +
            "d_code = #{dCode},\n" +
            "child_code = #{childCode},\n" +
            "try_flag = #{tryFlag}\n" +
            "WHERE supply_code = #{supplyCode}")
    void updateMzSupplyType(MzZdSupplyType mzZdSupplyType);

    @Update("update yz_supply_type set del_flag = #{delFlag} where supply_code = #{code} ")
    void delSupplyType(String code, String delFlag);

    @Update("update mz_zd_supply_type set del_flag = #{delFlag} where supply_code = #{code} ")
    void delMzSupplyType(String code, String delFlag);

    @Select("select charge_code, charge_amount, " +
            "charge_name = (select rtrim(name) from zd_charge_item where charge_code = code)\n" +
            "from yz_supply_charge where supply_code = #{code}")
    List<OrderZdMaintainController.SupplyCharge> getSupplyCharges(String code);

    @Select("select charge_code, amount as chargeAmount,\n" +
            "charge_name = (select rtrim(name) from zd_charge_item where charge_code = code)\n" +
            "from mz_zd_supply_charge where supply_code = #{code}")
    List<OrderZdMaintainController.SupplyCharge> getMzSupplyCharges(String code);

    @Delete("delete yz_supply_charge where supply_code = #{code} and charge_code = #{chargeCode}")
    void delSupplyCharge(String code, String chargeCode);

    @Delete("delete from mz_zd_supply_charge where supply_code = #{code} and charge_code = #{chargeCode}")
    void delMzSupplyCharge(String code, String chargeCode);

    @Insert("insert into yz_supply_charge(supply_code, charge_code, charge_amount)" +
            " values (#{code},#{chargeCode},#{chargeAmount})")
    void insertSupplyCharge(String code, String chargeCode, BigDecimal chargeAmount);

    @Insert("insert into mz_zd_supply_charge(supply_code, charge_code, amount)" +
            " values (#{code},#{chargeCode},#{chargeAmount})")
    void insertMzSupplyCharge(String code, String chargeCode, BigDecimal chargeAmount);

    @Delete("delete yz_supply_charge where supply_code = #{code}")
    void delSupplyChargeByCode(String code);

    @Delete("delete from mz_zd_supply_charge where supply_code = #{code}")
    void delMzSupplyChargeByCode(String code);

    @Select("select code,name,yz_class_code from zd_charge_class where yz_class_code is not null " +
            "and yz_class_code!='' and code not in ('C','M') and del_flag='0'")
    List<ZdChargeClass> getAllZdChargeClass();

    @Select("<script>" +
            "select code,yz_class_code from zd_charge_class " +
            "where del_flag='0' and yz_class_code in " +
            "(" +
            "<foreach collection='yzClassCodeList' item='yzClassCode' separator=','>" +
            "#{yzClassCode}" +
            "</foreach>" +
            ")" +
            "</script>")
    List<ZdChargeClass> getZdChargeClassByYzClass(List<String>  yzClassCodeList);

    @Select("select code,name,charge_amount,exec_unit,py_code " +
            "from zd_charge_item where class_code=#{classCode} " +
            "and code not in (select occ_code from yz_order_occurence)")
    List<ZdChargeItem> getZdChargeItemByClassCode(String classCode);

    @Insert("<script>" +
            "insert into yz_order_occurence (order_code, occ_code, amount) values " +
            "<foreach collection='list' item='item'  separator=','>" +
            "(#{item.orderCode},#{item.occCode},1)" +
            "</foreach>" +
            "</script>")
    void insertYzOrderOccurenceBatch(List<YzOrderOccurence> list);
}
