package com.jeeplus.test.program.configuration.projectList.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.mould.service.SerialnumTplService;
import com.jeeplus.test.oss.domain.WorkAttachment;
import com.jeeplus.test.oss.mapper.OssServiceMapper;
import com.jeeplus.test.program.configuration.projectList.domain.ProgramProjectListInfo;
import com.jeeplus.test.program.configuration.projectList.domain.ProgramProjectListLink;
import com.jeeplus.test.program.configuration.projectList.domain.ProgramProjectListMember;
import com.jeeplus.test.program.configuration.projectList.mapper.ProjectLinkMapper;
import com.jeeplus.test.program.configuration.projectList.mapper.ProjectListMapper;
import com.jeeplus.test.program.configuration.projectList.mapper.ProjectMemberMapper;
import com.jeeplus.test.program.configuration.projectList.service.dto.ContractDto;
import com.jeeplus.test.program.configuration.projectList.service.dto.ProjectListDto;
import com.jeeplus.test.workContract.domain.WorkContractInfo;
import com.jeeplus.test.workContract.mapper.WorkContractInfoMapper;
import com.jeeplus.test.workContract.service.WorkContractService;
import com.jeeplus.test.workContract.service.dto.WorkAttachmentDto;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ProjectListService {

    @Resource
    private ProjectListMapper projectListMapper;
    @Resource
    private ProjectLinkMapper projectLinkMapper;
    @Resource
    private ProjectMemberMapper projectMemberMapper;
    @Resource
    private WorkContractInfoMapper workContractInfoMapper;
    @Resource
    private OssServiceMapper ossServiceMapper;

    @Resource
    private SerialnumTplService serialnumTplService;
    @Resource
    private WorkContractService workContractService;

    public String save(ProjectListDto dto) throws Exception{
        if (StringUtils.isNotEmpty(dto.getId())) {
            return update(dto);
        }
        return add(dto);
    }

    public String add(ProjectListDto dto) throws Exception{
        ProgramProjectListInfo info = new ProgramProjectListInfo();
        //获取当前登录人信息
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        // 保存项目管理列表主表信息
        BeanUtils.copyProperties(dto, info);
        String id = UUID.randomUUID().toString().replace("-", "");
        //合同编号生成
        String no = serialnumTplService.genSerialNum(userDTO.getCompanyDTO().getId(), ProjectListDto.BIZ_CODE);
        info.setId(id);
        info.setNo(no);
        info.setCreateBy(userDTO.getId());
        info.setCreateDate(new Date());
        info.setUpdateBy(userDTO.getId());
        info.setUpdateDate(new Date());
        info.setDelFlag(0);
        projectListMapper.insert(info);
        // 保存项目直接对接人信息
        if (CollectionUtils.isNotEmpty(dto.getLinks())) {
            for (ProgramProjectListLink link : dto.getLinks()) {
                link.setId(UUID.randomUUID().toString().replace("-", ""));
                link.setType("1");
                link.setInfoId(id);
                link.setCreateBy(userDTO.getId());
                link.setCreateDate(new Date());
                link.setUpdateBy(userDTO.getId());
                link.setUpdateDate(new Date());
                link.setDelFlag(0);
                projectLinkMapper.insert(link);
            }
        }
        // 保存项目直接对接人信息
        if (CollectionUtils.isNotEmpty(dto.getLinkInfos())) {
            for (ProgramProjectListLink link : dto.getLinkInfos()) {
                link.setId(UUID.randomUUID().toString().replace("-", ""));
                link.setType("2");
                link.setInfoId(id);
                link.setCreateBy(userDTO.getId());
                link.setCreateDate(new Date());
                link.setUpdateBy(userDTO.getId());
                link.setUpdateDate(new Date());
                link.setDelFlag(0);
                projectLinkMapper.insert(link);
            }
        }
        // 保存项目管理列表成员信息
        if (CollectionUtils.isNotEmpty(dto.getMembers())) {
            for (ProgramProjectListMember member : dto.getMembers()) {
                member.setId(UUID.randomUUID().toString().replace("-", ""));
                member.setInfoId(id);
                member.setCreateBy(userDTO.getId());
                member.setCreateDate(new Date());
                member.setUpdateBy(userDTO.getId());
                member.setUpdateDate(new Date());
                member.setDelFlag(0);
                projectMemberMapper.insert(member);
            }
        }
        // 保存附件信息
        if (CollectionUtils.isNotEmpty(dto.getFiles())) {
            workContractService.saveFiles(dto.getFiles(), userDTO, id);
        }
        return id;
    }

    public String update(ProjectListDto dto) {
        //获取当前登录人信息
        UserDTO userDTO = UserUtils.getCurrentUserDTO();
        // 修改项目管理列表主表信息
        ProgramProjectListInfo info = new ProgramProjectListInfo();
        BeanUtils.copyProperties(dto, info);
        info.setUpdateBy(userDTO.getId());
        info.setUpdateDate(new Date());
        info.setDelFlag(0);
        projectListMapper.updateById(info);
        // 修改项目直接对接人信息(先删除原有数据，再保存)
        projectLinkMapper.deleteByInfoId(dto.getId());
        if (CollectionUtils.isNotEmpty(dto.getLinks())) {
            for (ProgramProjectListLink link : dto.getLinks()) {
                link.setId(UUID.randomUUID().toString().replace("-", ""));
                link.setType("1");
                link.setInfoId(dto.getId());
                link.setCreateBy(userDTO.getId());
                link.setCreateDate(new Date());
                link.setUpdateBy(userDTO.getId());
                link.setUpdateDate(new Date());
                link.setDelFlag(0);
                projectLinkMapper.insert(link);
            }
        }
        // 修改项目直接对接人信息
        if (CollectionUtils.isNotEmpty(dto.getLinkInfos())) {
            for (ProgramProjectListLink link : dto.getLinkInfos()) {
                link.setId(UUID.randomUUID().toString().replace("-", ""));
                link.setType("2");
                link.setInfoId(dto.getId());
                link.setCreateBy(userDTO.getId());
                link.setCreateDate(new Date());
                link.setUpdateBy(userDTO.getId());
                link.setUpdateDate(new Date());
                link.setDelFlag(0);
                projectLinkMapper.insert(link);
            }
        }
        // 修改项目管理列表成员信息(先删除原有数据，再保存)
        if (CollectionUtils.isNotEmpty(dto.getMembers())) {
            projectMemberMapper.deleteByInfoId(dto.getId());
            for (ProgramProjectListMember member : dto.getMembers()) {
                member.setId(UUID.randomUUID().toString().replace("-", ""));
                member.setInfoId(dto.getId());
                member.setCreateBy(userDTO.getId());
                member.setCreateDate(new Date());
                member.setUpdateBy(userDTO.getId());
                member.setUpdateDate(new Date());
                member.setDelFlag(0);
                projectMemberMapper.insert(member);
            }
        }
        // 修改附件信息
        if (CollectionUtils.isNotEmpty(dto.getFiles())) {
            workContractService.updateFiles(dto.getFiles(), userDTO, dto.getId());
        }
        return dto.getId();
    }

    public ProjectListDto findById(String id) {
        ProjectListDto dto = new ProjectListDto();
        // 查询主表信息
        ProgramProjectListInfo info = projectListMapper.selectById(id);
        BeanUtils.copyProperties(info, dto);
        // 查询项目直接对接人列表
        LambdaQueryWrapper<ProgramProjectListLink> linkWrapper = new LambdaQueryWrapper<>();
        linkWrapper.eq(ProgramProjectListLink::getInfoId, id);
        linkWrapper.eq(ProgramProjectListLink::getType, "1");
        linkWrapper.eq(ProgramProjectListLink::getDelFlag, 0);
        List<ProgramProjectListLink> links = projectLinkMapper.selectList(linkWrapper);
        if (CollectionUtils.isNotEmpty(links)) {
            dto.setLinks(links);
        }
        // 查询项目直接对接人列表
        LambdaQueryWrapper<ProgramProjectListLink> linkInfoWrapper = new LambdaQueryWrapper<>();
        linkInfoWrapper.eq(ProgramProjectListLink::getInfoId, id);
        linkInfoWrapper.eq(ProgramProjectListLink::getType, "2");
        linkInfoWrapper.eq(ProgramProjectListLink::getDelFlag, 0);
        List<ProgramProjectListLink> linkInfos = projectLinkMapper.selectList(linkInfoWrapper);
        if (CollectionUtils.isNotEmpty(linkInfos)) {
            dto.setLinkInfos(linkInfos);
        }
        // 查询成员列表
        LambdaQueryWrapper<ProgramProjectListMember> memberWrapper = new LambdaQueryWrapper<>();
        memberWrapper.eq(ProgramProjectListMember::getInfoId, id);
        memberWrapper.eq(ProgramProjectListMember::getDelFlag, 0);
        List<ProgramProjectListMember> members = projectMemberMapper.selectList(memberWrapper);
        if (CollectionUtils.isNotEmpty(members)) {
            dto.setMembers(members);
        }
        // 查询附件信息
        List<WorkAttachmentDto> files = workContractInfoMapper.findDtos(id);
        if (CollectionUtils.isNotEmpty(files)) {
            for (WorkAttachmentDto i : files) {
                i.setCreateBy(UserUtils.get(i.getBy()));
            }
            dto.setFiles(files);
        }
        return dto;
    }

    public String deleteById(String id) {
        projectListMapper.deleteById(id);
        projectLinkMapper.deleteByInfoId(id);
        projectMemberMapper.deleteByInfoId(id);
        // 附件删除
        LambdaQueryWrapper<WorkAttachment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WorkAttachment::getAttachmentId, id);
        ossServiceMapper.delete(wrapper);
        return "操作成功";
    }

    public IPage<WorkContractInfo> findContractPageList(ContractDto dto, Page<WorkContractInfo> page) {
        return projectListMapper.findContractPageList(page, dto);
    }

    public String updateStatusById(ProjectListDto dto) {
        projectListMapper.updateStatusById(dto.getId(), dto.getStatus());
        return "操作成功";
    }

    public IPage<ProgramProjectListInfo> list(Page<ProgramProjectListInfo> page, ProgramProjectListInfo info) throws Exception{
        QueryWrapper<ProgramProjectListInfo> wrapper = QueryWrapperGenerator.buildQueryCondition(info, ProgramProjectListInfo.class);
        wrapper.eq("a.del_flag", "0");
        if (info.getCreateDates() != null) {
            wrapper.between("a.create_date", info.getCreateDates()[0], info.getCreateDates()[1]);
        }
        IPage<ProgramProjectListInfo> pageList = projectListMapper.findPageList(page, wrapper);
        return pageList;
    }
}
