package com.ylx.massage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ylx.common.core.domain.model.LoginUser;
import com.ylx.common.utils.DateUtils;
import com.ylx.common.utils.bean.BeanUtils;
import com.ylx.massage.domain.vo.MaProjectListVo;
import com.ylx.massage.domain.vo.MaProjectSaveVo;
import com.ylx.massage.mapper.MaProjectMapper;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import com.ylx.massage.domain.MaProject;
import com.ylx.massage.service.IMaProjectService;

import javax.annotation.Resource;

/**
 * 服务项目Service业务层处理
 *
 * @author ylx
 * @date 2024-03-20
 */
@Service
public class MaProjectServiceImpl implements IMaProjectService
{
    @Resource
    private MaProjectMapper maProjectMapper;

    /**
     * 查询服务项目
     *
     * @param id 服务项目主键
     * @return 服务项目
     */
    @Override
    public MaProjectListVo selectMaProjectById(Long id)
    {
        MaProject maProject = maProjectMapper.selectMaProjectById(id);
        MaProjectListVo vo = new MaProjectListVo();
        BeanUtils.copyProperties(maProject, vo);
        vo.setProjectIsEnable(maProject.getProjectIsEnable() == 1 ? "启用" : "停止");
        return vo;
    }

    /**
     * 查询服务项目列表
     *
     * @param maProject 服务项目
     * @return 服务项目
     */
    @Override
    public List<MaProjectListVo> selectMaProjectList(MaProject maProject)
    {
        ArrayList<MaProjectListVo> vos = Lists.newArrayList();
        List<MaProject> maProjects = maProjectMapper.selectMaProjectList(maProject);
        if (maProjects.isEmpty()) {
            return vos;
        }
        maProjects.forEach(project->{
            MaProjectListVo vo = new MaProjectListVo();
            BeanUtils.copyProperties(project, vo);
            vo.setProjectIsEnable(project.getProjectIsEnable() == 1 ? "启用" : "停止");
            vos.add(vo);
        });
        return vos;
    }

    /**
     * 新增服务项目
     *
     * @param maProject 服务项目
     * @return 结果
     */
    @Override
    public int insertMaProject(MaProjectSaveVo maProjectVo, LoginUser loginUser) {
        MaProject maProject = new MaProject();
        BeanUtils.copyProperties(maProjectVo, maProject);

        if (maProjectVo.getId() == null) {
            // 新增
            maProject.setCreateUser(loginUser.getUser().getUserId());
            maProject.setUpdateUser(loginUser.getUser().getUserId());
            maProject.setCreateTime(DateUtils.getNowDate());
            return maProjectMapper.insertMaProject(maProject);
        } else {
            // 修改
            maProject.setUpdateUser(loginUser.getUser().getUserId());
            return maProjectMapper.updateMaProject(maProject);
        }

    }

    /**
     * 修改服务项目
     *
     * @param maProject 服务项目
     * @return 结果
     */
    @Override
    public int updateMaProject(MaProject maProject)
    {
        return maProjectMapper.updateMaProject(maProject);
    }

    /**
     * 批量删除服务项目
     *
     * @param ids 需要删除的服务项目主键
     * @return 结果
     */
    @Override
    public int deleteMaProjectByIds(String[] ids)
    {
        return maProjectMapper.deleteMaProjectByIds(ids);
    }

    /**
     * 删除服务项目信息
     *
     * @param id 服务项目主键
     * @return 结果
     */
    @Override
    public int deleteMaProjectById(Long id)
    {
        return maProjectMapper.deleteMaProjectById(id);
    }
}
