package com.fs.store.mapper;

import java.util.List;
import com.fs.store.domain.FsStoreOrderItem;
import com.fs.store.param.FsStoreOrderParam;
import com.fs.store.vo.FsStoreOrderItemExportVO;
import com.fs.store.vo.FsStoreOrderItemVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 订单详情Mapper接口
 *
 * @author fs
 * @date 2022-03-21
 */
public interface FsStoreOrderItemMapper
{
    /**
     * 查询订单详情
     *
     * @param itemId 订单详情ID
     * @return 订单详情
     */
    public FsStoreOrderItem selectFsStoreOrderItemById(Long itemId);

    /**
     * 查询订单详情列表
     *
     * @param fsStoreOrderItem 订单详情
     * @return 订单详情集合
     */
    public List<FsStoreOrderItem> selectFsStoreOrderItemList(FsStoreOrderItem fsStoreOrderItem);

    /**
     * 新增订单详情
     *
     * @param fsStoreOrderItem 订单详情
     * @return 结果
     */
    public int insertFsStoreOrderItem(FsStoreOrderItem fsStoreOrderItem);

    /**
     * 修改订单详情
     *
     * @param fsStoreOrderItem 订单详情
     * @return 结果
     */
    public int updateFsStoreOrderItem(FsStoreOrderItem fsStoreOrderItem);

    /**
     * 删除订单详情
     *
     * @param itemId 订单详情ID
     * @return 结果
     */
    public int deleteFsStoreOrderItemById(Long itemId);

    /**
     * 批量删除订单详情
     *
     * @param itemIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteFsStoreOrderItemByIds(Long[] itemIds);

    @Select("select * from fs_store_order_item where order_id=#{orderId}")
    List<FsStoreOrderItemVO> selectFsStoreOrderItemListByOrderId(Long orderId);
    @Select("select * from fs_store_order_item where order_id=#{orderId}")
    List<FsStoreOrderItemVO> selectMyFsStoreOrderItemListByOrderId(Long id);

    @Select({"<script> " +
            "select i.*,o.user_id,o.status, o.real_name,o.user_phone,o.user_address,o.create_time,o.pay_time,o.delivery_sn,o.delivery_name,o.delivery_id, c.company_name ,cu.nick_name as company_user_nick_name ,cu.phonenumber as company_usere_phonenumber,o.upload_time ,CASE WHEN o.certificates IS NULL OR o.certificates = '' THEN 0 ELSE 1 END AS is_upload   " +
            " ,p.title as package_name,cts.name as scheduleName from fs_store_order_item i left join fs_store_order o on o.id=i.order_id left join fs_user u on o.user_id=u.user_id  " +
            " left join fs_store_product_package p on o.package_id=p.package_id left join company c on c.company_id=o.company_id left join company_user cu on cu.user_id=o.company_user_id left join company_tcm_schedule cts on cts.id = o.schedule_id " +
            "where 1=1 " +
            "<if test = 'maps.orderCode != null and  maps.orderCode !=\"\"    '> " +
            "and o.order_code like CONCAT('%',#{maps.orderCode},'%') " +
            "</if>" +
            "<if test = 'maps.deliveryId != null    '> " +
            "and o.delivery_id =#{maps.deliveryId} " +
            "</if>" +
            "<if test = 'maps.nickname != null and  maps.nickname !=\"\"     '> " +
            "and u.nickname like CONCAT('%',#{maps.nickname},'%') " +
            "</if>" +
            "<if test = 'maps.phone != null and  maps.phone !=\"\"     '> " +
            "and u.phone like CONCAT('%',#{maps.phone},'%') " +
            "</if>" +
            "<if test = 'maps.userPhone != null and  maps.userPhone !=\"\"     '> " +
            "and o.user_phone like CONCAT('%',#{maps.userPhone},'%') " +
            "</if>" +
            "<if test = 'maps.status != null    '> " +
            "and o.status =#{maps.status} " +
            "</if>" +
            "<if test = 'maps.companyId != null    '> " +
            "and o.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.isHealth != null and maps.isHealth !=  \"\"  '> " +
            "and o.company_id is null " +
            "</if>" +
            "<if test = 'maps.notHealth != null and maps.notHealth !=  \"\"  '> " +
            "and o.company_id is not null " +
            "</if>" +
            "<if test = 'maps.companyUserId != null    '> " +
            "and o.company_user_id =#{maps.companyUserId} " +
            "</if>" +
            "<if test = 'maps.companyUserNickName != null and  maps.companyUserNickName !=  \"\" '> " +
            "and cu.nick_name like concat('%', #{maps.companyUserNickName}, '%') " +
            "</if>" +
            "<if test = 'maps.orderType != null    '> " +
            "and o.order_type =#{maps.orderType} " +
            "</if>" +
            "<if test = 'maps.createTimeList != null    '> " +
            " AND date_format(o.create_time,'%y%m%d') &gt;= date_format(#{maps.createTimeList[0]},'%y%m%d') " +
            " AND date_format(o.create_time,'%y%m%d') &lt;= date_format(#{maps.createTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.payTimeList != null    '> " +
            " AND date_format(o.pay_time,'%y%m%d') &gt;= date_format(#{maps.payTimeList[0]},'%y%m%d') " +
            " AND date_format(o.pay_time,'%y%m%d') &lt;= date_format(#{maps.payTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.deliverySendTimeList != null    '> " +
            " AND date_format(o.delivery_send_time,'%y%m%d') &gt;= date_format(#{maps.deliverySendTimeList[0]},'%y%m%d') " +
            " AND date_format(o.delivery_send_time,'%y%m%d') &lt;= date_format(#{maps.deliverySendTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.deliveryImportTimeList != null    '> " +
            " AND date_format(o.delivery_import_time,'%y%m%d') &gt;= date_format(#{maps.deliveryImportTimeList[0]},'%y%m%d') " +
            " AND date_format(o.delivery_import_time,'%y%m%d') &lt;= date_format(#{maps.deliveryImportTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.isUpload != null and maps.isUpload == 0    '> " +
            "and o.certificates is null  " +
            "</if>" +
            "<if test = 'maps.isUpload != null and maps.isUpload == 1    '> " +
            "and o.certificates is not null " +
            "</if>" +
            "<if test = 'maps.scheduleId != null    '> " +
            "and o.schedule_id =#{maps.scheduleId} " +
            "</if>" +
            " order by o.id desc limit 50000"+
            "</script>"})
    List<FsStoreOrderItemExportVO> selectFsStoreOrderItemListExportVO(@Param("maps")FsStoreOrderParam fsStoreOrder);

    @Select({"<script> " +
            "select count(0) from fs_store_order_item i left join fs_store_order o on o.id=i.order_id left join fs_user u on o.user_id=u.user_id  " +
            " left join fs_store_product_package p on o.package_id=p.package_id left join company c on c.company_id=o.company_id left join company_user cu on cu.user_id=o.company_user_id left join company_tcm_schedule cts on cts.id = o.schedule_id " +
            "where 1=1 " +
            "<if test = 'maps.orderCode != null and  maps.orderCode !=\"\"    '> " +
            "and o.order_code like CONCAT('%',#{maps.orderCode},'%') " +
            "</if>" +
            "<if test = 'maps.deliveryId != null    '> " +
            "and o.delivery_id =#{maps.deliveryId} " +
            "</if>" +
            "<if test = 'maps.nickname != null and  maps.nickname !=\"\"     '> " +
            "and u.nickname like CONCAT('%',#{maps.nickname},'%') " +
            "</if>" +
            "<if test = 'maps.phone != null and  maps.phone !=\"\"     '> " +
            "and u.phone like CONCAT('%',#{maps.phone},'%') " +
            "</if>" +
            "<if test = 'maps.realName != null and  maps.realName !=\"\"     '> " +
            "and o.real_name like CONCAT('%',#{maps.realName},'%') " +
            "</if>" +
            "<if test = 'maps.userPhone != null and  maps.userPhone !=\"\"     '> " +
            "and o.user_phone like CONCAT('%',#{maps.userPhone},'%') " +
            "</if>" +
            "<if test = 'maps.status != null    '> " +
            "and o.status =#{maps.status} " +
            "</if>" +
            "<if test = 'maps.companyId != null    '> " +
            "and o.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.isHealth != null and maps.isHealth !=  \"\"  '> " +
            "and o.company_id is null " +
            "</if>" +
            "<if test = 'maps.notHealth != null and maps.notHealth !=  \"\"  '> " +
            "and o.company_id is not null " +
            "</if>" +
            "<if test = 'maps.companyUserId != null    '> " +
            "and o.company_user_id =#{maps.companyUserId} " +
            "</if>" +
            "<if test = 'maps.companyUserNickName != null and  maps.companyUserNickName !=  \"\" '> " +
            "and cu.nick_name like concat('%', #{maps.companyUserNickName}, '%') " +
            "</if>" +
            "<if test = 'maps.orderType != null    '> " +
            "and o.order_type =#{maps.orderType} " +
            "</if>" +
            "<if test = 'maps.createTimeList != null    '> " +
            " AND date_format(o.create_time,'%y%m%d') &gt;= date_format(#{maps.createTimeList[0]},'%y%m%d') " +
            " AND date_format(o.create_time,'%y%m%d') &lt;= date_format(#{maps.createTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.payTimeList != null    '> " +
            " AND date_format(o.pay_time,'%y%m%d') &gt;= date_format(#{maps.payTimeList[0]},'%y%m%d') " +
            " AND date_format(o.pay_time,'%y%m%d') &lt;= date_format(#{maps.payTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.deliverySendTimeList != null    '> " +
            " AND date_format(o.delivery_send_time,'%y%m%d') &gt;= date_format(#{maps.deliverySendTimeList[0]},'%y%m%d') " +
            " AND date_format(o.delivery_send_time,'%y%m%d') &lt;= date_format(#{maps.deliverySendTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.deliveryImportTimeList != null    '> " +
            " AND date_format(o.delivery_import_time,'%y%m%d') &gt;= date_format(#{maps.deliveryImportTimeList[0]},'%y%m%d') " +
            " AND date_format(o.delivery_import_time,'%y%m%d') &lt;= date_format(#{maps.deliveryImportTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.isUpload != null and maps.isUpload == 0    '> " +
            "and o.certificates is null  " +
            "</if>" +
            "<if test = 'maps.isUpload != null and maps.isUpload == 1    '> " +
            "and o.certificates is not null " +
            "</if>" +
            "<if test = 'maps.scheduleId != null    '> " +
            "and o.schedule_id =#{maps.scheduleId} " +
            "</if>" +
            " order by o.id desc "+
            "</script>"})
    Long itemsCount(@Param("maps")FsStoreOrderParam fsStoreOrder);

    @Update("update fs_store_order_item set order_code=#{orderCode} where order_id=#{orderId} ")
    int updateFsStoreOrderCode(@Param("orderId")Long orderId, @Param("orderCode")String orderCode);

    @Select("select i.* from fs_store_order o LEFT JOIN fs_store_order_item i ON o.id = i.order_id  " +
            "where o.create_time >= '2023-10-11 00:00:00' AND o.create_time <= '2023-10-13 00:00:00' and o.status = 1 and i.json_info LIKE '%sb10324801+10104103+10104003%'  and  o.extend_order_id is null ")
    List<FsStoreOrderItem> selectOrderItem();

    @Select("select id from fs_store_order where status = 1 and extend_order_id is null ")
    List<Long> selectOrderIdByNoErp();

    @Update("update fs_store_order_item set json_info=#{jsonInfo} where order_id=#{orderId} ")
    int updateJsonInfo(FsStoreOrderItem fsStoreOrderItem);
}
