package com.ruoyi.syzy.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.syzy.domain.BCustomerMarjors;
import com.ruoyi.syzy.mapper.BCustomerMarjorsMapper;
import com.ruoyi.syzy.service.IBCustomerMarjorsService;

/**
 * 客户专业收藏Service业务层处理
 * 
 * @author mingxue
 * @date 2021-07-27
 */
@Service
public class BCustomerMarjorsServiceImpl implements IBCustomerMarjorsService {
    @Autowired
    private BCustomerMarjorsMapper bCustomerMarjorsMapper;

    /**
     * 查询客户专业收藏
     * 
     * @param id 客户专业收藏ID
     * @return 客户专业收藏
     */
    @Override
    public BCustomerMarjors selectBCustomerMarjorsById(Long id) {
        return bCustomerMarjorsMapper.selectBCustomerMarjorsById(id);
    }

    /**
     * 查询客户专业收藏列表
     * 
     * @param bCustomerMarjors 客户专业收藏
     * @return 客户专业收藏
     */
    @Override
    public List<BCustomerMarjors> selectBCustomerMarjorsList(BCustomerMarjors bCustomerMarjors) {
        return bCustomerMarjorsMapper.selectBCustomerMarjorsList(bCustomerMarjors);
    }

    /**
     * 新增客户专业收藏
     * 
     * @param bCustomerMarjors 客户专业收藏
     * @return 结果
     */
    @Override
    public int insertBCustomerMarjors(BCustomerMarjors bCustomerMarjors) {
        bCustomerMarjors.setCreateTime(DateUtils.getNowDate());
        return bCustomerMarjorsMapper.insertBCustomerMarjors(bCustomerMarjors);
    }

    /**
     * 修改客户专业收藏
     * 
     * @param bCustomerMarjors 客户专业收藏
     * @return 结果
     */
    @Override
    public int updateBCustomerMarjors(BCustomerMarjors bCustomerMarjors) {
        return bCustomerMarjorsMapper.updateBCustomerMarjors(bCustomerMarjors);
    }

    /**
     * 批量删除客户专业收藏
     * 
     * @param ids 需要删除的客户专业收藏ID
     * @return 结果
     */
    @Override
    public int deleteBCustomerMarjorsByIds(Long[] ids) {
        return bCustomerMarjorsMapper.deleteBCustomerMarjorsByIds(ids);
    }

    /**
     * 删除客户专业收藏信息
     * 
     * @param id 客户专业收藏ID
     * @return 结果
     */
    @Override
    public int deleteBCustomerMarjorsById(Long id) {
        return bCustomerMarjorsMapper.deleteBCustomerMarjorsById(id);
    }

    @Override
    public void add(String customerCode, Long marjorId) {
        BCustomerMarjors exam = new BCustomerMarjors();
        exam.setCustomerCode(customerCode);
        exam.setMarjorId(marjorId);
        List<BCustomerMarjors> coll = selectBCustomerMarjorsList(exam);
        if (CollectionUtils.isNotEmpty(coll)) {
            BCustomerMarjors entity = coll.get(0);
            entity.setStatus(1);
            entity.setCreateTime(new Date());
            updateBCustomerMarjors(entity);
        } else {
            BCustomerMarjors entity = new BCustomerMarjors();
            entity.setCustomerCode(customerCode);
            entity.setMarjorId(marjorId);
            entity.setStatus(1);
            entity.setCreateTime(new Date());
            insertBCustomerMarjors(entity);
        }
    }

    @Override
    public void remove(String customerCode, Long id) {
        BCustomerMarjors entity = selectBCustomerMarjorsById(id);
        entity.setStatus(0);
        updateBCustomerMarjors(entity);
    }

    @Override
    public List<JSONObject> listMyByPage(String customerCode) {
        return bCustomerMarjorsMapper.listMyByPage(customerCode);
    }

}
