package com.tofly.project.service.impl;


import com.haiqiu.tools.excel.utils.ExcelUtils;
import com.tofly.common.constant.Constant;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.common.service.IFileInfoService;
import com.tofly.project.entity.ProjectRegistration;
import com.tofly.project.entity.vo.ProjectOtherSupervisionVo;
import com.tofly.project.entity.vo.ProjectSupervisionVo;
import com.tofly.project.service.ProjectRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.project.mapper.ProjectSupervisionMapper;
import com.tofly.project.entity.ProjectSupervision;
import com.tofly.project.entity.dto.ProjectSupervisionDto;
import com.tofly.project.service.ProjectSupervisionService;
import org.springframework.stereotype.Service;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.io.Serializable;

/**
 * 工程监管(ProjectSupervision)表服务实现类
 *
 * @author HaiQiu
 * @since 2022-11-14 11:56:47
 */
@Service("projectSupervisionService")
public class ProjectSupervisionServiceImpl extends ServiceImpl<ProjectSupervisionMapper, ProjectSupervision> implements ProjectSupervisionService {


    @Value("${dcsw.ftp.project.projectSupervision:/dcsw/project/projectSupervision}")
    private String path;
    @Autowired
    private IFileInfoService fileInfoService;

    @Autowired
    private ProjectRegistrationService projectRegistrationService;


    /**
     * 根据ID查询
     *
     * @param id id
     * @return 数据
     */
    @Override
    public ProjectSupervision get(Serializable id) {
        ProjectSupervision supervision = getById(id);
        if (!ObjectUtils.isEmpty(supervision)) {
            ProjectRegistration registration = projectRegistrationService.getById(supervision.getProjectId());
            supervision.setRegistration(registration);
        }
        return supervision;
    }

    /**
     * 新增
     *
     * @param projectSupervisionDto 实体
     * @param files
     */
    @Override
    public void insert(ProjectSupervisionDto projectSupervisionDto, List<MultipartFile> files) {
        ProjectSupervision projectSupervision = convert(projectSupervisionDto);
        projectSupervision.setAnnex(fileInfoService.uploadFiles(files,projectSupervisionDto.getAnnex()
                ,projectSupervisionDto.getAnnex(),path,"FLOW_PROJECT_CONSTRUCTION"));
        if (!save(projectSupervision)) {
            throw new RuntimeException("新增失败");
        }
    }

    /**
     * 修改
     *
     * @param projectSupervisionDto 实体
     * @param files
     */
    @Override
    public void edit(ProjectSupervisionDto projectSupervisionDto, List<MultipartFile> files) {
        ProjectSupervision projectSupervision = convert(projectSupervisionDto);
        projectSupervision.setAnnex(fileInfoService.uploadFiles(files,projectSupervisionDto.getAnnex()
                ,projectSupervisionDto.getAnnex(),path,"FLOW_PROJECT_SUPERVISION"));
        if (!updateById(projectSupervision)) {
            throw new RuntimeException("修改失败");
        }
    }

    /**
     * 实体转换
     *
     * @param projectSupervisionDto dto实体
     */
    private ProjectSupervision convert(ProjectSupervisionDto projectSupervisionDto) {
        ProjectSupervision projectSupervision = new ProjectSupervision();
        BeanUtils.copyProperties(projectSupervisionDto, projectSupervision);
        //自动获取监管人
        if (projectSupervision.getSupervise()==null){
            projectSupervision.setSupervise(SecurityUtils.getUserId());
        }
        //自动获取监管时间
        if (projectSupervision.getSuperviseTime()==null){
            projectSupervision.setSuperviseTime(new Date());
        }
        return projectSupervision;
    }

    /**
     * 批量删除
     *
     * @param idList 数据ID集合
     */
    @Override
    public void delete(List<Long> idList) {
        if (!removeByIds(idList)) {
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 生成模板下载
     */
    @Override
    public void template() {
        ProjectSupervision projectSupervision = new ProjectSupervision();
        ExcelUtils.createTemplate("工程监管", "工程监管",
                "工程监管", ProjectSupervision.class, Collections.singletonList(projectSupervision));
    }

    /**
     * 数据导入
     *
     * @param file 文件
     */
    @Override
    public void importData(MultipartFile file) {
        List<ProjectSupervision> datas = ExcelUtils.getExcelToList("工程监管", file, ProjectSupervision.class);
        if (!CollectionUtils.isEmpty(datas)) {
            saveBatch(datas);
        }
    }

    /**
     * 分页查询
     *
     * @param page               分页
     * @param projectSupervision 查询wrapper
     * @return 数据集合
     */
    @Override
    public Page<ProjectSupervisionVo> pageList(Page page, ProjectSupervisionVo projectSupervision) {
        QueryWrapper<Object> wrapper = getQueryWrapper(projectSupervision);
        return baseMapper.pageList(page, wrapper);
    }

    /**
     * 构建查询条件
     * @param projectOtherSupervision
     * @return
     */
    private QueryWrapper<Object> getQueryWrapper(ProjectSupervisionVo projectOtherSupervision) {
        QueryWrapper<Object> wrapper = new QueryWrapper<>();
        wrapper.between(projectOtherSupervision.getStartTime() != null && projectOtherSupervision.getEndTime() != null, "os.CREATE_TIME"
                        , projectOtherSupervision.getStartTime(), projectOtherSupervision.getEndTime()).orderByDesc("os.CREATE_TIME")
                .ne("os.DELETED", Constant.DELETED)
                .eq(projectOtherSupervision.getProjectType()!=null,"p.DATA_TYPE", projectOtherSupervision.getProjectType())
                .and(StringUtils.hasText(projectOtherSupervision.getSearchStr()), w -> {
                    w.like( "p.NAME", projectOtherSupervision.getSearchStr())
                            .or()
                            .like("p.ADDRESS", projectOtherSupervision.getSearchStr())
                            .or()
                            .like( "p.ID", projectOtherSupervision.getSearchStr());
                });
        return wrapper;
    }


    /**
     * 数据导出
     *
     * @param page               分页
     * @param projectSupervision 查询模型
     * @param isAll              是否导出全部数据，true是，false否
     */
    @Override
    public void export(Page page, ProjectSupervision projectSupervision, Boolean isAll) {
        Page<ProjectSupervision> pageData = new Page<>();
        if (isAll == null || !isAll) {
            pageData = page(page, new QueryWrapper<>(projectSupervision)).addOrder(page.getOrders());
        } else {
            pageData.setRecords(list(new QueryWrapper<>(projectSupervision)));
        }
        //导出数据
        ExcelUtils.getListToExcel(ProjectSupervision.class, pageData.getRecords(), "工程监管");
    }

    /**
     * 查询所有数据
     *
     * @param projectSupervision 查询条件
     * @return 查询所有数据
     */
    @Override
    public List<ProjectSupervision> allList(ProjectSupervision projectSupervision) {
        return list(new QueryWrapper<>(projectSupervision));
    }
}

