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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.utils.StringUtils;
import com.project.zcustom.domain.addional.LargeHousingLand;
import com.project.zcustom.mapper.logistics.PlatHousingLandMapper;
import com.project.zcustom.service.logistics.ILargeHousingLandService;
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
public class LargeHousingLandServiceImpl extends ServiceImpl<PlatHousingLandMapper, LargeHousingLand> implements ILargeHousingLandService {

    @Autowired
    private PlatHousingLandMapper platHousingLandMapper;


    public Map<String, Object> getStockOfEstateResources(String appOrg){
        String[] strArr = {"供电所用房", "物资仓储用房", "办公用房", "周转用房"};
        List<LargeHousingLand> arr = new ArrayList<>();
        for (int i = 0; i < 4; i++){
            LargeHousingLand land = new LargeHousingLand();
            land.setHouseNum(0L);
            land.setHouseAcreage(BigDecimal.valueOf(0));
            land.setType((long) i);
            land.setTypeName(strArr[i]);
            arr.add(land);
        }
        List<LargeHousingLand> list = platHousingLandMapper.getStockOfEstateResources(appOrg);
        Map<String, Object> map = new HashMap<>();
        BigDecimal housingAcreage = new BigDecimal(0);
        Long housingNum = 0L;
        for (LargeHousingLand it : list){
            housingAcreage = housingAcreage.add(it.getHouseAcreage());
            housingNum += it.getHouseNum();
            arr.get(it.getType().intValue()).setHouseAcreage(it.getHouseAcreage());
            arr.get(it.getType().intValue()).setHouseNum(it.getHouseNum());
        }
        map.put("pieChart", arr);
        map.put("housingNum", housingNum);
        map.put("housingAcreage", housingAcreage);
        return map;
    }

    public Map<String, Object> getStockOfLandResources(String appOrg){
        String[] strArr = {"供电所土地", "物资仓储土地", "办公土地", "周转土地"};
        List<LargeHousingLand> arr = new ArrayList<>();
        for (int i = 0; i < 4; i++){
            LargeHousingLand land = new LargeHousingLand();
            land.setLandNum(0L);
            land.setLandAcreage(BigDecimal.valueOf(0));
            land.setType((long) i);
            land.setTypeName(strArr[i]);
            arr.add(land);
        }
        List<LargeHousingLand> list = platHousingLandMapper.getStockOfLandResources(appOrg);
        Map<String, Object> map = new HashMap<>();
        BigDecimal landAcreage = new BigDecimal(0);
        Long landNum = 0L;
        for (LargeHousingLand it : list){
            landAcreage = landAcreage.add(it.getLandAcreage());
            landNum += it.getLandNum();
            arr.get(it.getType().intValue()).setLandAcreage(it.getLandAcreage());
            arr.get(it.getType().intValue()).setLandNum(it.getLandNum());
        }
        map.put("chart", arr);
        map.put("landNum", landNum);
        map.put("landAcreage", landAcreage);
        return map;
    }

    public Map<String, Object> getInvestFinishState(String appOrg){
        return platHousingLandMapper.getInvestFinishState(appOrg);
    }

    /**
     * 分页查询房产土地列表
     *
     * @param largePlatHousingLand 房产土地
     * @return 房产土地集合
     */
    @Override
    public IPage<LargeHousingLand> pagingLargePlatHousingLandList(LargeHousingLand largePlatHousingLand) {
        IPage<LargeHousingLand> page = new Page<>(largePlatHousingLand.getPageNum(), largePlatHousingLand.getPageSize());
        LambdaQueryWrapper<LargeHousingLand> lqw = getLambdaQueryWrapper(largePlatHousingLand);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeHousingLand::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

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

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

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

        Map<String, Long> mapType = new HashMap<>();
        mapType.put("供电所", 0L);
        mapType.put("物资仓储", 1L);
        mapType.put("办公", 2L);
        mapType.put("周转", 3L);

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

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

            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.setAppOrg(cell.getStringCellValue());
                        }
                        break;
                    case 1:
                        if (cell != null && cell.getCellType() == CellType.NUMERIC) {
                            a.setHouseNum((long) cell.getNumericCellValue());
                        }
                        break;
                    case 2:
                        if (cell != null && cell.getCellType() == CellType.NUMERIC) {
                            a.setHouseAcreage(BigDecimal.valueOf(cell.getNumericCellValue()));
                        }
                        break;
                    case 3:
                        if (cell != null && cell.getCellType() == CellType.NUMERIC) {
                            a.setLandNum((long) cell.getNumericCellValue());
                        }
                        break;
                    case 4:
                        if (cell != null && cell.getCellType() == CellType.NUMERIC) {
                            a.setLandAcreage(BigDecimal.valueOf(cell.getNumericCellValue()));
                        }
                        break;
                    case 5:
                        if (cell != null && cell.getCellType() == CellType.STRING) {
                            a.setType(mapType.get(cell.getStringCellValue()));
                        }
                        break;
                    default:
                        break;
                }
            }

            data.add(a);
        }

        workbook.close();
        return data;
    }

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargeHousingLand> getLambdaQueryWrapper(LargeHousingLand largePlatHousingLand) {
        LambdaQueryWrapper<LargeHousingLand> lqw = new LambdaQueryWrapper<LargeHousingLand>();
        if (StringUtils.isNotNull(largePlatHousingLand.getAppOrg())){
            lqw.eq(LargeHousingLand::getAppOrg ,largePlatHousingLand.getAppOrg());
        }
        if (StringUtils.isNotNull(largePlatHousingLand.getType())){
            lqw.eq(LargeHousingLand::getType ,largePlatHousingLand.getType());
        }
        return lqw;
    }
}
