package com.fs.store.mapper;

import java.util.List;
import com.fs.store.domain.FsStoreAfterSales;
import com.fs.store.param.FsStoreAfterSalesQueryParam;
import com.fs.store.vo.FsStoreAfterSalesQueryVO;
import com.fs.store.vo.FsStoreAfterSalesVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 售后记录Mapper接口
 *
 * @author fs
 * @date 2022-03-15
 */
public interface FsStoreAfterSalesMapper
{
    /**
     * 查询售后记录
     *
     * @param id 售后记录ID
     * @return 售后记录
     */
    public FsStoreAfterSales selectFsStoreAfterSalesById(Long id);

    /**
     * 查询售后记录列表
     *
     * @param fsStoreAfterSales 售后记录
     * @return 售后记录集合
     */
    public List<FsStoreAfterSales> selectFsStoreAfterSalesList(FsStoreAfterSales fsStoreAfterSales);

    /**
     * 新增售后记录
     *
     * @param fsStoreAfterSales 售后记录
     * @return 结果
     */
    public int insertFsStoreAfterSales(FsStoreAfterSales fsStoreAfterSales);

    /**
     * 修改售后记录
     *
     * @param fsStoreAfterSales 售后记录
     * @return 结果
     */
    public int updateFsStoreAfterSales(FsStoreAfterSales fsStoreAfterSales);

    /**
     * 删除售后记录
     *
     * @param id 售后记录ID
     * @return 结果
     */
    public int deleteFsStoreAfterSalesById(Long id);

    /**
     * 批量删除售后记录
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteFsStoreAfterSalesByIds(Long[] ids);

    @Select({"<script> " +
            "select *  from fs_store_after_sales  " +
            "where 1=1 " +
            "<if test = 'maps.status != null and maps.status ==1   '> " +
            "and sales_status = 0 " +
            "</if>" +
            "<if test = 'maps.status != null and maps.status ==2   '> " +
            "and sales_status = 3 " +
            "</if>" +
            "<if test = 'maps.userId != null    '> " +
            "and user_id = #{maps.userId} " +
            "</if>" +
            "order by create_time desc "+
            "</script>"})
    List<FsStoreAfterSalesQueryVO>  selectFsStoreAfterSalesListQuery(@Param("maps") FsStoreAfterSalesQueryParam storeAfterSalesParam);

    @Select({"<script> " +
            "select s.*,o.delivery_status,o.delivery_id,u.phone as user_phone,c.company_name ,cu.nick_name as company_user_nick_name ,cu.phonenumber as company_usere_phonenumber  from fs_store_after_sales s INNER join fs_store_order o on o.order_code=s.order_code left join fs_user u on s.user_id=u.user_id left join company c on c.company_id=s.company_id left join company_user cu on cu.user_id=s.company_user_id " +
            "where 1=1 " +
            "<if test = 'maps.status != null    '> " +
            "and s.status = #{maps.status} " +
            "</if>" +
            "<if test = 'maps.salesStatus != null    '> " +
            "and s.sales_status = #{maps.salesStatus} " +
            "</if>" +
            "<if test = 'maps.orderStatus != null    '> " +
            "and s.order_status = #{maps.orderStatus} " +
            "</if>" +
            "<if test = 'maps.orderCode != null and  maps.orderCode !=  \"\" '> " +
            "and o.order_code like concat('%', #{maps.orderCode}, '%') " +
            "</if>" +
            "<if test = 'maps.deliveryStatus != null    '> " +
            "and o.delivery_status = #{maps.deliveryStatus} " +
            "</if>" +
            "<if test = 'maps.serviceType != null    '> " +
            "and s.service_type = #{maps.serviceType} " +
            "</if>" +
            "<if test = 'maps.companyId != null    '> " +
            "and s.company_id = #{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.companyUserId != null    '> " +
            "and s.company_user_id = #{maps.companyUserId} " +
            "</if>" +
            "<if test = 'maps.deliverySn != null and  maps.deliverySn !=  \"\" '> " +
            " and ( o.delivery_id like concat('%', #{maps.deliverySn}, '%') or s.delivery_sn like concat('%', #{maps.deliverySn}, '%')) " +
            "</if>" +
            "<if test = 'maps.companyUserNickName != null and  maps.companyUserNickName !=  \"\" '> " +
            "and cu.nick_name like concat('%', #{maps.companyUserNickName}, '%') " +
            "</if>" +
            "<if test = 'maps.beginTime != null and maps.beginTime != \"\"   '> " +
            " AND date_format(s.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.consigneePhone != null and  maps.consigneePhone !=\"\"     '> " +
            "and o.user_phone like CONCAT('%',#{maps.consigneePhone},'%') " +
            "</if>" +
            "<if test = 'maps.endTime != null and maps.endTime != \"\"   '> " +
            " AND date_format(s.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.deptId != null    '> " +
            "  AND (o.dept_id = #{maps.deptId} OR o.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) " +
            "</if>" +
            " ${maps.params.dataScope} "+
            "order by s.create_time desc "+
            "</script>"})
    List<FsStoreAfterSalesVO> selectFsStoreAfterSalesListVO(@Param("maps") FsStoreAfterSales fsStoreAfterSales);


    @Select({"<script> " +
            "select ifnull(count(1),0) from fs_store_after_sales   " +
            "where user_id=#{userId}  and sales_status =#{salesStatus} " +
            "</script>"})
    Integer selectFsStoreAfterSalesCount(@Param("userId")long userId, @Param("salesStatus")int salesStatus);
    @Select({"<script> " +
            "select s.*,o.delivery_status,o.delivery_id,u.phone as user_phone,c.company_name ,cu.nick_name as company_user_nick_name ,cu.phonenumber as company_usere_phonenumber  from fs_store_after_sales s INNER join fs_store_order o on o.order_code=s.order_code left join fs_user u on s.user_id=u.user_id left join company c on c.company_id=s.company_id left join company_user cu on cu.user_id=s.company_user_id " +
            "where 1=1 " +
            "<if test = 'orderCode != null    '> " +
            "and s.order_code = #{orderCode} " +
            "</if>" +
            "</script>"})
    List<FsStoreAfterSalesVO> selectFsStoreAfterSalesVOByOrderCode(@Param("orderCode")String  orderCode);
    @Select({"<script> " +
            "select s.*  from fs_store_after_sales s   " +
            "where s.sales_status=0 and s.status=0 and date_format(date_add(s.create_time, interval 24 hour),'%Y-%m-%d %H') &gt;= date_format(now(),'%Y-%m-%d %H')  " +
            "</script>"})
    List<FsStoreAfterSales> selectFsStoreAfterSalesByDoAudit();

    @Update("update fs_store_after_sales set delivery_sn =#{deliverySn},delivery_name = #{deliveryName},status = #{status} where id = #{id}")
    int updateDeliveryById(FsStoreAfterSales fsStoreAfterSales);
}
