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.zcustom.controller.core.StringUtils;
import com.project.zcustom.domain.addional.LargeHousingLand;
import com.project.zcustom.mapper.logistics.PlatHousingLandMapper;
import com.project.zcustom.service.logistics.ILargeHousingLandService;
import com.sun.rowset.internal.Row;
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[] strArr = {"办公用房", "周转用房", "生产管理", "教育培训用房", "交流干部住房", "单身公寓/单身宿舍"};
        List<LargeHousingLand> arr = new ArrayList<>();
        for (int i = 0; i < 6; i++){
            LargeHousingLand land = new LargeHousingLand();
            land.setNum(0L);
            land.setAcreage(BigDecimal.valueOf(0));
            land.setType((long) i);
            land.setTypeName(strArr[i]);
            arr.add(land);
        }
        List<LargeHousingLand> list = platHousingLandMapper.getStockOfEstateResources();
        Map<String, Object> map = new HashMap<>();
        BigDecimal housingAcreage = new BigDecimal(0);
        Long housingNum = 0L;
        for (LargeHousingLand it : list){
            housingAcreage = housingAcreage.add(it.getAcreage());
            housingNum += it.getNum();
            arr.get(it.getType().intValue()).setAcreage(it.getAcreage());
            arr.get(it.getType().intValue()).setNum(it.getNum());
        }
        map.put("pieChart", arr);
        map.put("housingNum", housingNum);
        map.put("housingAcreage", housingAcreage);
        return map;
    }

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

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

    /**
     * 分页查询房产土地列表
     *
     * @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);
    }


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