package com.fs.hisStore.mapper;

import java.util.List;

import com.fs.his.param.FsStoreAfterSalesParam;
import com.fs.his.vo.FsStoreAfterSalesExcelVO;
import com.fs.his.vo.FsStoreAfterSalesLogsVO;
import com.fs.hisStore.domain.FsStoreAfterSalesItemScrm;
import com.fs.hisStore.domain.FsStoreAfterSalesScrm;
import com.fs.hisStore.param.FsStoreAfterSalesQueryParam;
import com.fs.hisStore.vo.FsStoreAfterSalesQueryVO;
import com.fs.hisStore.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 FsStoreAfterSalesScrmMapper
{
    /**
     * 查询售后记录
     *
     * @param id 售后记录ID
     * @return 售后记录
     */
    public FsStoreAfterSalesScrm selectFsStoreAfterSalesById(Long id);

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

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

    /**
     * 修改售后记录
     *
     * @param fsStoreAfterSales 售后记录
     * @return 结果
     */
    public int updateFsStoreAfterSales(FsStoreAfterSalesScrm 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_scrm  " +
            "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_scrm s INNER join fs_store_order_scrm 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.params != null and maps.params != \"\"   '> " +
            "<if test = 'maps.params.beginTime != null and maps.params.beginTime != \"\"   '> " +
            " AND date_format(s.create_time,'%y%m%d') &gt;= date_format(#{maps.params.beginTime},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.params.endTime != null and maps.params.endTime != \"\"   '> " +
            " AND date_format(s.create_time,'%y%m%d') &lt;= date_format(#{maps.params.endTime},'%y%m%d') " +
            "</if>" +
            "</if>" +
            "<if test = 'maps.consigneePhone != null and  maps.consigneePhone !=\"\"     '> " +
            "and o.user_phone like CONCAT('%',#{maps.consigneePhone},'%') " +
            "</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") FsStoreAfterSalesScrm fsStoreAfterSales);


    @Select({"<script> " +
            "select ifnull(count(1),0) from fs_store_after_sales_scrm   " +
            "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_scrm s INNER join fs_store_order_scrm 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_scrm 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<FsStoreAfterSalesScrm> selectFsStoreAfterSalesByDoAudit();

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

    @Select({"<script> " +
            "select so.*,st.store_name,us.nick_name,us.phone,fso.order_code,fso.delivery_sn as delivery_id,fpo.package_name ,c.company_name,cu.nick_name as company_user_nick_name FROM fs_store_after_sales_scrm so " +
            " LEFT JOIN fs_store_scrm st ON so.store_id =st.store_id " +
            " LEFT JOIN fs_user us ON us.user_id=so.user_id " +
            " LEFT JOIN fs_store_order_scrm fso ON fso.order_code=so.order_code  "+
            " left join company c on so.company_id = c.company_id " +
            " left join fs_package_order fpo on fso.order_code = fpo.order_sn " +
            " left join company_user cu on so.company_user_id = cu.user_id   WHERE so.is_del=0 "+
            "    <if test=\"maps.storeId != null \"> and so.store_id = #{maps.storeId}</if>\n" +
            "    <if test=\"maps.refundAmount != null \"> and so.refund_amount = #{maps.refundAmount}</if>\n" +
            "    <if test=\"maps.refundType != null \"> and so.refund_type = #{maps.refundType}</if>\n" +
            "    <if test=\"maps.reasons != null  and maps.reasons != ''\"> and so.reasons = #{maps.reasons}</if>\n" +
            "    <if test=\"maps.explains != null  and maps.explains != ''\"> and so.explains = #{maps.explains}</if>\n" +
            "    <if test=\"maps.deliveryCode != null  and maps.deliveryCode != ''\"> and so.delivery_code = #{maps.deliveryCode}</if>\n" +
            "    <if test=\"maps.deliverySn != null  and maps.deliverySn != ''\"> and so.delivery_sn = #{maps.deliverySn}</if>\n" +
            "    <if test=\"maps.deliveryName != null  and maps.deliveryName != ''\"> and so.delivery_name like concat('%', #{maps.deliveryName}, '%')</if>\n" +
            "    <if test=\"maps.status != null \"> and so.status = #{maps.status}</if>\n" +
            "    <if test=\"maps.salesStatus != null \"> and so.sales_status = #{maps.salesStatus}</if>\n" +
            "    <if test=\"maps.phone != null \"> and us.phone = #{maps.phone}</if>\n" +
            "    <if test=\"maps.orderStatus != null \"> and so.order_status = #{maps.orderStatus}</if>\n" +
            "    <if test=\"maps.isDel != null  and isDel != ''\"> and so.is_del = #{maps.isDel}</if>\n" +
            "    <if test=\"maps.userId != null \"> and so.user_id = #{maps.userId}</if>\n" +
            "<if test = 'maps.createTimeList != null    '> " +
            " AND date_format(so.create_time,'%y%m%d') &gt;= date_format(#{maps.createTimeList[0]},'%y%m%d') " +
            " AND date_format(so.create_time,'%y%m%d') &lt;= date_format(#{maps.createTimeList[1]},'%y%m%d') " +
            "</if>" +
            "<if test = 'maps.companyUserNickName != null and  maps.companyUserNickName !=  \"\" '> " +
            " and cu.nick_name like concat(#{maps.companyUserNickName}, '%') " +
            "</if>" +
            "    <if test=\"maps.consignee != null  and maps.consignee != ''\"> and so.consignee = #{maps.consignee}</if>\n" +
            "    <if test=\"maps.phoneNumber != null  and maps.phoneNumber != ''\"> and so.phone_number = #{maps.phoneNumber}</if>\n" +
            "    <if test=\"maps.address != null  and maps.address != ''\"> and so.address = #{maps.address}</if>\n" +
            "<if test = 'maps.companyId != null  and   maps.companyId != \"-1\" '> " +
            "and so.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = ' maps.companyId == \"-1\" '> " +
            "and so.company_id is null " +
            "</if>" +
            "    <if test=\"maps.companyUserId != null \"> and so.company_user_id = #{maps.companyUserId}</if>\n" +
            "    <if test=\"maps.isPackage != null \"> and so.is_package = #{maps.isPackage}</if>"+
            "    <if test=\"maps.orderCode != null \"> and fso.order_code= #{maps.orderCode}</if>"+
            "<if test = 'maps.deptId != null    '> " +
            "  AND (so.dept_id = #{maps.deptId} OR so.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) " +
            "</if>" +
            " order by so.id desc "+
            "</script>"})
    List<FsStoreAfterSalesExcelVO> selectFsStoreAfterSalesExcelListVO(@Param("maps") FsStoreAfterSalesParam fsStoreAfterSales);

    @Select("select so.*,st.store_name,us.nick_name,us.phone,fso.order_code " +
            "FROM fs_store_after_sales_scrm so " +
            "LEFT JOIN fs_store_scrm st ON so.store_id =st.store_id " +
            "LEFT JOIN fs_user us ON us.user_id=so.user_id " +
            "LEFT JOIN fs_store_order_scrm fso ON fso.id=so.order_code " +
            "WHERE so.id=#{id} ")
    FsStoreAfterSalesVO selectFsStoreAfterSalesVOById(Long id);

    @Select("select * FROM fs_store_after_sales_item_scrm WHERE store_after_sales_id =#{id}")
    List<FsStoreAfterSalesItemScrm> selectFsStoreAfterSalesItemById(Long id);

    @Select("select a.*,c.company_name,s.store_name FROM fs_store_after_sales_logs_scrm a left join company c on c.company_id = a.company_id " +
            " left join fs_store_scrm s on s.store_id = a.store_id WHERE a.store_after_sales_id =#{id}")
    List<FsStoreAfterSalesLogsVO> selectFsStoreAfterSalesLogById(Long id);

    @Select(" SELECT id FROM fs_store_order_scrm WHERE order_code =#{id}")
    Long selectFsOrderIdByCode(Long id);
}
