package com.tofly.project.service.impl;


import com.haiqiu.tools.excel.utils.ExcelUtils;
import com.tofly.common.constant.Constant;
import com.tofly.common.service.IFileInfoService;
import com.tofly.project.entity.ProjectRegistration;
import com.tofly.project.entity.vo.ProjectConstructionVo;
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.ProjectConstructionMapper;
import com.tofly.project.entity.ProjectConstruction;
import com.tofly.project.entity.dto.ProjectConstructionDto;
import com.tofly.project.service.ProjectConstructionService;
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.List;
import java.io.Serializable;

/**
 * 工程施工(ProjectConstruction)表服务实现类
 *
 * @author HaiQiu
 * @since 2022-11-14 11:56:44
 */
@Service("projectConstructionService")
public class ProjectConstructionServiceImpl extends ServiceImpl<ProjectConstructionMapper, ProjectConstruction> implements ProjectConstructionService {


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

    @Autowired
    private ProjectRegistrationService projectRegistrationService;

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

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

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

    /**
     * 实体转换
     *
     * @param projectConstructionDto dto实体
     */
    private ProjectConstruction convert(ProjectConstructionDto projectConstructionDto) {
        ProjectConstruction projectConstruction = new ProjectConstruction();
        BeanUtils.copyProperties(projectConstructionDto, projectConstruction);
        return projectConstruction;
    }

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

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

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

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

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

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

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

