package com.ruoyi.ibs.task.service.impl;

import com.github.pagehelper.Page;
import com.ruoyi.common.core.page.TableDataPageInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ibs.task.domain.dto.WorkRecordDTO;
import com.ruoyi.ibs.task.domain.dto.WorkTemplateDTO;
import com.ruoyi.ibs.task.domain.entity.WorkRecord;
import com.ruoyi.ibs.task.domain.entity.WorkTemplate;
import com.ruoyi.ibs.task.domain.vo.WorkRecordVO;
import com.ruoyi.ibs.task.domain.vo.WorkTemplateVO;
import com.ruoyi.ibs.task.mapper.WorkRecordMapper;
import com.ruoyi.ibs.task.mapper.WorkTemplateMapper;
import com.ruoyi.ibs.task.service.WorkRecordService;
import com.ruoyi.ibs.task.service.WorkTemplateService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.impl.SysPostServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 工作清单模板Service业务层处理
 *
 */
@Service
public class WorkTemplateServiceImpl implements WorkTemplateService {

    @Autowired
    private WorkTemplateMapper workTemplateMapper;

    @Autowired
    private ISysPostService sysPostService;

    /**
     * 获取工作清单模板列表
     *
     * 1、根据当前登录者的dept_id查询所有post_ids
     * 2、根据post_ids查询所有模板
     *
     * @return
     */
    @Override
    public TableDataPageInfo<WorkTemplateVO> getWorkTemplateList(WorkTemplateDTO workTemplateDTO) {
        List<SysPost> sysPosts = sysPostService.selectPostList(new SysPost());
        if (sysPosts == null || sysPosts.isEmpty()){
            return PageUtils.getDataTable(new ArrayList<>(), new Page<>());
        }
        List<Long> postIds = sysPosts.stream()
                .map(SysPost::getPostId)
                .collect(Collectors.toList());
        //分页 这个查询工具类只能进一次sql，因此放在这里
        Page<Object> page = PageUtils.startPage();
        List<WorkTemplateVO> list = workTemplateMapper.selectWorkTemplateList(workTemplateDTO,postIds);
        return PageUtils.getDataTable(list, page);
    }

    /**
     * 添加工作清单模板
     */
    @Override
    public int insertWorkTemplate(WorkTemplate workTemplate) {
        workTemplate.setCreateBy(SecurityUtils.getUsername());
        return workTemplateMapper.insertWorkTemplate(workTemplate);
    }

    /**
     * 修改工作清单模板
     */
    @Override
    public int updateWorkTemplate(WorkTemplate workTemplate) {
        return workTemplateMapper.updateWorkTemplate(workTemplate);
    }

    /**
     * 删除工作清单模板
     */
    @Override
    public int deleteWorkTemplateByIds(Long[] ids) {
        return workTemplateMapper.deleteWorkTemplateByIds(ids);
    }
}
