package com.storlead.sales.service.impl.customer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.customer.BusinessAddOrDeleteDTO;
import com.storlead.sales.pojo.entity.customer.CustomerUserFollowEntity;
import com.storlead.sales.mapper.customer.CustomerUserFollowMapper;
import com.storlead.sales.service.customer.CustomerUserFollowService;
import com.storlead.framework.mybatis.service.impl.MyBaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 客户重点关注表 服务实现类
 * </p>
 *
 * @author YPZ
 * @since 2024-06-12
 */
@Service
public class CustomerUserFollowServiceImpl extends MyBaseServiceImpl<CustomerUserFollowMapper, CustomerUserFollowEntity> implements CustomerUserFollowService {

    @Override
    public Result<?> addCustomerUserFollow(BusinessAddOrDeleteDTO businessAddOrDeleteDTO) {
        //查看是否存在客户id和用户id相同的数据，如果存在不操作，不存在添加
        LambdaQueryWrapper<CustomerUserFollowEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CustomerUserFollowEntity::getCustomerId,businessAddOrDeleteDTO.getCustomerId());
        queryWrapper.eq(CustomerUserFollowEntity::getUserId, LoginUserUtil.getCurrentUserId());
        List<CustomerUserFollowEntity> customerUserFollowEntities = this.list(queryWrapper);
        if (!CollectionUtils.isEmpty(customerUserFollowEntities)){
            return Result.result("已重点关注");
        }
        CustomerUserFollowEntity customerUserFollowEntity = new CustomerUserFollowEntity();
        customerUserFollowEntity.setCustomerId(businessAddOrDeleteDTO.getCustomerId());
        customerUserFollowEntity.setUserId(LoginUserUtil.getCurrentUserId());
        boolean isOk = this.save(customerUserFollowEntity);
        if (isOk){
            return Result.result("已重点关注");
        }
        return Result.error("关注失败");
    }

    @Override
    public Result<?> deleteCustomerUserFollow(BusinessAddOrDeleteDTO businessAddOrDeleteDTO) {
//        LambdaUpdateWrapper<CustomerUserFollowEntity> updateWrapper = new LambdaUpdateWrapper<>();
        LambdaQueryWrapper<CustomerUserFollowEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerUserFollowEntity::getCustomerId,businessAddOrDeleteDTO.getCustomerId());
        wrapper.eq(CustomerUserFollowEntity::getUserId,LoginUserUtil.getCurrentUserId());
        int count = baseMapper.delete(wrapper);
        if (count > 0){
            return Result.result("取消关注成功");
        }
        return Result.error("取消关注失败");
    }
}
