package com.xunmei.system.service.impl;

import java.util.List;

import com.xunmei.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xunmei.system.service.ISysOrgService;

import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunmei.common.core.web.page.TableDataInfo;
import com.xunmei.system.mapper.SysWorkTimeMonthMapper;
import com.xunmei.system.domain.SysWorkTimeMonth;
import com.xunmei.system.service.ISysWorkTimeMonthService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-14
 */
@Service
public class SysWorkTimeMonthServiceImpl extends ServiceImpl<SysWorkTimeMonthMapper, SysWorkTimeMonth> implements ISysWorkTimeMonthService {
    @Autowired
    private SysWorkTimeMonthMapper sysWorkTimeMonthMapper;
    @Autowired
    private ISysOrgService orgService;

    @Override
    public TableDataInfo<SysWorkTimeMonth> selectPage(SysWorkTimeMonth sysWorkTimeMonth) {

        Page<SysWorkTimeMonth> page;
        //分页
        if (sysWorkTimeMonth.getPageNum() != null && sysWorkTimeMonth.getPageSize() != null) {
            page = new Page<>(sysWorkTimeMonth.getPageNum(), sysWorkTimeMonth.getPageSize());
        } else {
            page = new Page<>();
        }
        //查询条件
        QueryWrapper<SysWorkTimeMonth> query = new QueryWrapper<>(sysWorkTimeMonth);
        //下穿
        if (sysWorkTimeMonth.getCheckSub()) {
            List<Long> ids = orgService.selectCheckSubOrgIdList(sysWorkTimeMonth.getOrgId());
            //清空前端传递的org_id
            sysWorkTimeMonth.setOrgId(null);
            //添加in条件
            query.in("org_id", ids);
        }
        //时间范围查询
        if (sysWorkTimeMonth.getParams().get("beginTime") != null && sysWorkTimeMonth.getParams().get("endTime") != null) {
            query.between("create_time", sysWorkTimeMonth.getParams().get("beginTime"), sysWorkTimeMonth.getParams().get("endTime"));
        }
        //获取数据
        page = sysWorkTimeMonthMapper.selectPage(page, query);
        //抓换为TableDataInfo适配前端
        return TableDataInfo.build(page);
    }


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public SysWorkTimeMonth selectSysWorkTimeMonthById(String id) {
        return sysWorkTimeMonthMapper.selectById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param sysWorkTimeMonth 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<SysWorkTimeMonth> selectSysWorkTimeMonthList(SysWorkTimeMonth sysWorkTimeMonth) {
        return sysWorkTimeMonthMapper.selectList(new QueryWrapper<>(sysWorkTimeMonth));
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param sysWorkTimeMonth 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertSysWorkTimeMonth(SysWorkTimeMonth sysWorkTimeMonth) {
        sysWorkTimeMonth.setCreateTime(DateUtils.getNowDate());
        return sysWorkTimeMonthMapper.insert(sysWorkTimeMonth);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param sysWorkTimeMonth 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateSysWorkTimeMonth(SysWorkTimeMonth sysWorkTimeMonth) {
        sysWorkTimeMonth.setUpdateTime(DateUtils.getNowDate());
        return sysWorkTimeMonthMapper.updateById(sysWorkTimeMonth);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysWorkTimeMonthByIds(String[] ids) {
        return sysWorkTimeMonthMapper.deleteBatchIds(Arrays.asList((ids)));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteSysWorkTimeMonthById(String id) {
        return sysWorkTimeMonthMapper.deleteById(id);
    }
}
