package com.ruoyi.dxp.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dxp.mapper.ServiceItemMapper;
import com.ruoyi.dxp.domain.ServiceItem;
import com.ruoyi.dxp.service.IServiceItemService;

/**
 * 服务字段Service业务层处理
 *
 * @author jy
 * @date 2022-11-29
 */
@Service
public class ServiceItemServiceImpl implements IServiceItemService {
    @Autowired
    private ServiceItemMapper serviceItemMapper;

    /**
     * 查询服务字段
     *
     * @param id 服务字段主键
     * @return 服务字段
     */
    @Override
    public ServiceItem selectServiceItemById(Long id) {
        return serviceItemMapper.selectServiceItemById(id);
    }

    /**
     * 查询服务字段列表
     *
     * @param serviceItem 服务字段
     * @return 服务字段
     */
    @Override
    public List<ServiceItem> selectServiceItemList(ServiceItem serviceItem) {
        return serviceItemMapper.selectServiceItemList(serviceItem);
    }

    /**
     * 新增服务字段
     *
     * @param serviceItem 服务字段
     * @return 结果
     */
    @Override
    public int insertServiceItem(ServiceItem serviceItem) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        serviceItem.setCreateBy(loginUser.getUsername());
        serviceItem.setCreateTime(DateUtils.getNowDate());
        return serviceItemMapper.insertServiceItem(serviceItem);
    }

    /**
     * 修改服务字段
     *
     * @param serviceItem 服务字段
     * @return 结果
     */
    @Override
    public int updateServiceItem(ServiceItem serviceItem) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        serviceItem.setUpdateBy(loginUser.getUsername());
        serviceItem.setUpdateTime(DateUtils.getNowDate());
        return serviceItemMapper.updateServiceItem(serviceItem);
    }

    /**
     * 批量删除服务字段
     *
     * @param ids 需要删除的服务字段主键
     * @return 结果
     */
    @Override
    public int deleteServiceItemByIds(Long[] ids) {
        return serviceItemMapper.deleteServiceItemByIds(ids);
    }

    /**
     * 删除服务字段信息
     *
     * @param id 服务字段主键
     * @return 结果
     */
    @Override
    public int deleteServiceItemById(Long id) {
        return serviceItemMapper.deleteServiceItemById(id);
    }

    /**
     * 通过类型和服务id删除服务明细
     *
     * @param serviceType  服务类型
     * @param serviceId 服务id
     * @return
     */
    @Override
    public void deleteServiceItemByTypeAndDataId(String serviceType, Long serviceId) {
        ServiceItem req = new ServiceItem();
        req.setServiceType(serviceType);
        req.setServiceId(serviceId);
        List<ServiceItem> serviceItems = serviceItemMapper.selectServiceItemList(req);
        if (serviceItems != null && serviceItems.size() > 0) {
            List<Long> ids = this.serviceItemMapper.selectServiceItemList(req).stream().map(serviceItem -> {
                return serviceItem.getId();
            }).collect(Collectors.toList());

            this.serviceItemMapper.deleteServiceItemByIds(ids.toArray(new Long[0]));
        }
    }
}
