package com.fs.his.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fs.his.domain.FsIntegralOrder;
import com.fs.his.domain.FsIntegralOrderDf;
import com.fs.his.param.FsIntegralOrderListUParam;
import com.fs.his.param.FsIntegralOrderParam;
import com.fs.his.vo.FsIntegralOrderListUVO;
import com.fs.his.vo.FsIntegralOrderListVO;
import com.fs.his.vo.FsIntegralOrderPVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 积分商品订单Mapper接口
 *
 * @author fs
 * @date 2023-11-02
 */
public interface FsIntegralOrderMapper extends BaseMapper<FsIntegralOrder>
{
    /**
     * 查询积分商品订单
     *
     * @param orderId 积分商品订单主键
     * @return 积分商品订单
     */
    public FsIntegralOrder selectFsIntegralOrderByOrderId(Long orderId);

    /**
     * 查询积分商品订单列表
     *
     * @param fsIntegralOrder 积分商品订单
     * @return 积分商品订单集合
     */
    public List<FsIntegralOrder> selectFsIntegralOrderList(FsIntegralOrder fsIntegralOrder);

    /**
     * 新增积分商品订单
     *
     * @param fsIntegralOrder 积分商品订单
     * @return 结果
     */
    public int insertFsIntegralOrder(FsIntegralOrder fsIntegralOrder);

    /**
     * 修改积分商品订单
     *
     * @param fsIntegralOrder 积分商品订单
     * @return 结果
     */
    public int updateFsIntegralOrder(FsIntegralOrder fsIntegralOrder);

    /**
     * 删除积分商品订单
     *
     * @param orderId 积分商品订单主键
     * @return 结果
     */
    public int deleteFsIntegralOrderByOrderId(Long orderId);

    /**
     * 批量删除积分商品订单
     *
     * @param orderIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsIntegralOrderByOrderIds(Long[] orderIds);
    @Select({"<script> select order_id, order_code, user_id, user_name, user_phone, user_address, item_json, integral,pay_money,is_pay,pay_time,pay_type, status, delivery_code, delivery_name, delivery_sn, delivery_time, create_time,qw_user_id,company_user_id,company_id, remark from fs_integral_order " +
            "<where>  \n" +
            "            <if test=\"orderCode != null  and orderCode != ''\"> and order_code = #{orderCode}</if>\n" +
            "            <if test=\"userName != null  and userName != ''\"> and user_name like concat('%', #{userName}, '%')</if>\n" +
            "            <if test=\"userPhone != null  and userPhone != ''\"> and user_phone = #{userPhone}</if>\n" +
            "            <if test=\"integral != null  and integral != ''\"> and integral = #{integral}</if>\n" +
            "            <if test=\"payMoney != null  and payMoney != ''\"> and pay_money = #{payMoney}</if>\n" +
            "            <if test=\"isPay != null  and isPay != ''\"> and is_pay = #{isPay}</if>\n" +
            "            <if test=\"payTime != null  and payTime != ''\"> and pay_time = #{payTime}</if>\n" +
            "            <if test=\"payType != null  and payType != ''\"> and pay_type = #{payType}</if>\n" +
            "            <if test=\"status != null  and status != ''\"> and status = #{status}</if>\n" +
            "            <if test=\"deliverySn != null  and deliverySn != ''\"> and delivery_sn like concat('%', #{deliverySn}, '%')</if>\n" +
            "            <if test=\"qwUserId != null  and qwUserId != ''\"> and qw_user_id = #{qwUserId}</if>\n" +
            "            <if test=\"companyUserId != null  and companyUserId != ''\"> and company_user_id = #{companyUserId}</if>\n" +
            "            <if test=\"companyId != null  and companyId != ''\"> and company_id = #{companyId}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(create_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(create_time) &lt;= DATE(#{eTime})</if>\n" +
            "        </where> order by order_id desc"+
            "</script>"})
    List<FsIntegralOrderListVO> selectFsIntegralOrderListVO(FsIntegralOrderParam fsIntegralOrder);
    @Select({"<script> " +
            "select o.*  from fs_integral_order o  " +
            "where 1=1  " +
            "<if test = 'maps.userId != null   '> " +
            "and o.user_id = #{maps.userId}  " +
            "</if>" +
            "<if test = 'maps.status != null and maps.status != 0   '> " +
            "and o.status = #{maps.status}  " +
            "</if>" +
            " order by o.order_id desc "+
            "</script>"})
    List<FsIntegralOrderListUVO> selectFsIntegralOrderListUVO(@Param("maps")FsIntegralOrderListUParam param);
    @Select("select *from fs_integral_order where order_code=#{orderCode}")
    FsIntegralOrder selectFsIntegralOrderByOrderCode(String orderCode);

    @Select("UPDATE fs_integral_order SET `status`=3 WHERE `status`=2 and DATE(delivery_time) < DATE_SUB(CURDATE(), INTERVAL 15 DAY)")
    void updatePackageOrderStatusByDeliveryTime();

    @Select("select fio.*,fu.nick_name,fu.phone from fs_integral_order fio left join fs_user fu on fu.user_id = fio.user_id where order_id=#{orderId} ")
    FsIntegralOrderPVO selectFsIntegralOrderPVO(Long orderId);

    int cancelOrder(@Param("orderId") Long orderId);

    int finishOrder(@Param("orderId") Long orderId,@Param("oldStatus") Integer status);

    List<FsIntegralOrder> findOrderByIds(@Param("orderIds") List<Long> orderIds);

    List<FsIntegralOrderListVO> selectFsIntegralOrderListJn(FsIntegralOrderParam fsIntegralOrder);
}
