package com.project.zcustom.service.unit.impl;

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.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.core.domain.search.PageParamVo;
import com.project.common.utils.StringUtils;
import com.project.zcustom.domain.addional.LargeProject;
import com.project.zcustom.mapper.unit.LargeProjectMapper;
import com.project.zcustom.service.unit.ILargeProjectService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;

/**
 * 工程项目Service业务层处理
 *
 * @author change
 * @date 2024-12-30
 */
@Service
public class LargeProjectServiceImpl extends ServiceImpl<LargeProjectMapper, LargeProject> implements ILargeProjectService {

    @Autowired
    private LargeProjectMapper largeProjectMapper;

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargeProject> getLambdaQueryWrapper(LargeProject largeProject) {
        LambdaQueryWrapper<LargeProject> lqw = new LambdaQueryWrapper<LargeProject>();
                        if (StringUtils.isNotBlank(largeProject.getProjectName())){
                        lqw.like(LargeProject::getProjectName ,largeProject.getProjectName());
                    }
                        if (StringUtils.isNotNull(largeProject.getAppOrg())){
                        lqw.eq(LargeProject::getAppOrg ,largeProject.getAppOrg());
                    }
                        if (StringUtils.isNotBlank(largeProject.getConnectFlag())){
                        lqw.eq(LargeProject::getConnectFlag ,largeProject.getConnectFlag());
                    }
        return lqw;
    }

    /**
     * 分页查询工程项目列表
     *
     * @param largeProject 工程项目
     * @return 工程项目集合
     */
    @Override
    public IPage<LargeProject> pagingLargeProjectList(LargeProject largeProject) {
        IPage<LargeProject> page = new Page<>(largeProject.getPageNum(), largeProject.getPageSize());
        LambdaQueryWrapper<LargeProject> lqw = getLambdaQueryWrapper(largeProject);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeProject::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询工程项目列表
     *
     * @param lqw
     * @return 工程项目集合
     */
    @Override
    public IPage<LargeProject> pagingLargeProjectList(PageParamVo pageParamVo, LambdaQueryWrapper<LargeProject> lqw) {
        IPage<LargeProject> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeProject::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询工程项目列表
     *
     * @param largeProject 工程项目
     * @return 工程项目
     */
    @Override
    public List<LargeProject> selectLargeProjectList(LargeProject largeProject) {
        LambdaQueryWrapper<LargeProject> lqw = getLambdaQueryWrapper(largeProject);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeProject::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询工程项目列表
     *
     * @param largeProject 工程项目
     * @return 工程项目
     */
    @Override
    public List<LargeProject> selectLargeProjectList(LargeProject largeProject,Long limitNum) {
        LambdaQueryWrapper<LargeProject> lqw = getLambdaQueryWrapper(largeProject);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeProject::getCreateTime);
        lqw.last("limit "+limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 工程项目
     */
    @Override
    public LargeProject getLargeProjectByField(String field, String filedValue) {
        QueryWrapper<LargeProject> lqw = new QueryWrapper<LargeProject>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 工程项目
     */
    @Override
    public List<LargeProject> selectLargeProjectListByField(String field, String filedValue,Long limitNum) {
        QueryWrapper<LargeProject> lqw = new QueryWrapper<LargeProject>();
        lqw.eq(field, filedValue);
        lqw.last("limit "+limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 工程项目
     */
    @Override
    public List<LargeProject> selectLargeProjectListByField(String field, String filedValue) {
        QueryWrapper<LargeProject> lqw = new QueryWrapper<LargeProject>();
        lqw.eq(field, filedValue);
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field 字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue,String tips, Long id){
        QueryWrapper<LargeProject> lqw = new QueryWrapper<LargeProject>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if(this.getOne(lqw,false) != null){
            throw new RuntimeException(tips + "已存在");
        }
    }
    /**
     *  真删接口
     */
    @Override
    public void reallyDeleteLargeProject(LambdaQueryWrapper<LargeProject> lqw){
        baseMapper.reallyDeleteByWrapper(lqw);
    }

    public Long getProjectNum(String appOrg){
        return largeProjectMapper.getProjectNum(appOrg);
    }

    public List<LargeProject> getProjectNumX(String appOrg) {
        return largeProjectMapper.getProjectNumX(appOrg);
    }
    public List<LargeProject> getProjectNumY(String appOrg) {
        return largeProjectMapper.getProjectNumY(appOrg);
    }

    public List<LargeProject> getProjectByAppOrg(String appOrg){
        return largeProjectMapper.getProjectByAppOrg(appOrg);
    }

    public List<LargeProject> getCameraNumList(String appOrg){
        return largeProjectMapper.getCameraNumList(appOrg);
    }

    public int getProjectNumThisWeek(String appOrg){
        return largeProjectMapper.getProjectNumThisWeek(appOrg);
    }

    public Map<String, Object> getProjectNumZ(){
        List<String> appOrg = new ArrayList<>(Arrays.asList("1A003", "1B001", "1C001", "1D001", "1E001", "1F001", "1K001", "1L001", "1H001", "1M001", "1J001"));
        List<LargeProject> list = largeProjectMapper.getProjectNumZ(appOrg);
        Map<String, Object> map = new HashMap<>();
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for (String app : appOrg){
            int x = 0, y = 0;
            for (LargeProject project : list){
                if (project.getAppOrg().equals(app) || project.getAppParentOrg().equals(app)){
                    if (project.getSubdivision().equals(1L)){
                        x++;
                    }
                    else {
                        y++;
                    }
                }
            }
            list1.add(x);
            list2.add(y);
            map.put("list1", list1);
            map.put("list2", list2);
        }
        return map;
    }

    public int upload(MultipartFile file){
        try {
            List<LargeProject> data = parseExcel(file.getInputStream());
            largeProjectMapper.upload(data);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    // 解析 Excel 文件
    private List<LargeProject> parseExcel(InputStream inputStream) throws IOException {
        Workbook workbook = new XSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0); // 获取第一个表单
        Iterator<Row> rowIterator = sheet.iterator();

        List<LargeProject> data = new ArrayList<>();

        // 跳过表头
        if (rowIterator.hasNext()) {
            rowIterator.next();
        }

        Map<String, String> mapType = new HashMap<>();
        mapType.put("在建", "0");
        mapType.put("施工", "2");
        Map<String, Long> mapSubdivision = new HashMap<>();
        mapSubdivision.put("可研评审", 1L);
        mapSubdivision.put("初设评审", 2L);
        mapSubdivision.put("政府立项备案", 3L);
        mapSubdivision.put("土地手续", 4L);
        mapSubdivision.put("用地规划", 5L);
        mapSubdivision.put("工程规划", 6L);
        mapSubdivision.put("设计招标", 7L);
        mapSubdivision.put("施工招标", 8L);
        mapSubdivision.put("监理招标", 9L);
        mapSubdivision.put("施工许可", 10L);
        mapSubdivision.put("基础施工", 11L);
        mapSubdivision.put("主体工程", 12L);
        mapSubdivision.put("室内外装修", 13L);
        mapSubdivision.put("室外工程", 14L);

        // 遍历每一行
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            LargeProject a = new LargeProject();

            for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
                Cell cell = row.getCell(i);

                switch (i) {
                    case 0:
                        if (cell != null && cell.getCellType() == CellType.STRING) {
                            a.setProjectName(cell.getStringCellValue());
                        }
                        break;
                    case 1:
                        if (cell != null) {
                            if (cell.getCellType() == CellType.STRING){
                                a.setAppOrg(cell.getStringCellValue());
                            }
                            if (cell.getCellType() == CellType.NUMERIC){
                                a.setAppOrg(String.valueOf((int)cell.getNumericCellValue()));
                            }
                        }
                        break;
                    case 2:
                        if (cell != null && cell.getCellType() == CellType.STRING) {
                            a.setType(mapType.get(cell.getStringCellValue()));
                        }
                        break;
                    case 3:
                        if (cell != null && cell.getCellType() == CellType.STRING) {
                            a.setSubdivision(mapSubdivision.get(cell.getStringCellValue().trim().replaceAll("\\s+", "")));
                        }
                        break;
                    case 4:
                        if (cell != null && cell.getCellType() == CellType.NUMERIC) {
                            a.setAbscissa(BigDecimal.valueOf(cell.getNumericCellValue()));
                        }
                        break;
                    case 5:
                        if (cell != null && cell.getCellType() == CellType.NUMERIC) {
                            a.setOrdinate(BigDecimal.valueOf(cell.getNumericCellValue()));
                        }
                        break;
                    case 6:
                        if (cell != null && cell.getCellType() == CellType.STRING) {
                            a.setSupervisionUnit(cell.getStringCellValue());
                        }
                        break;
                    case 7:
                        if (cell != null && cell.getCellType() == CellType.STRING) {
                            a.setConstructionUnit(cell.getStringCellValue());
                        }
                        break;
                    default:
                        break;
                }
            }

            data.add(a);
        }

        workbook.close();
        return data;
    }
}
