package com.sysu.admin.controller.crop.xls;

import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.sysu.admin.controller.city.DistrictRepository;
import com.sysu.admin.controller.crop.CropLand;
import com.sysu.admin.controller.crop.class_item.CropClassItem;
import com.sysu.admin.controller.crop.class_item.CropItemService;
import com.sysu.admin.controller.crop.period.CropPeriod;
import com.sysu.admin.controller.crop.period.CropPeriodService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class CropExcelService {

    @Autowired
    private CropItemService cropItemService;
    @Autowired
    private DistrictRepository districtRepository;
    @Autowired
    private CropPeriodService cropPeriodService;

    @Transactional()
    public List<CropExcel> cropLandToCropExcel(List<CropLand> cropLandList){
        List<CropExcel> res = new ArrayList<>(cropLandList.size());
        for(CropLand cropLand : cropLandList){
            CropExcel cropExcel = new CropExcel();
            cropExcel.setId(cropLand.getId());
            cropExcel.setName(cropLand.getName());
            cropExcel.setAddress(cropLand.getAddress());
            if(cropLand.getCropType() != null) {
                CropClassItem cropClassItem = cropItemService.findOne(cropLand.getCropType());
                cropExcel.setCropType(cropClassItem == null ? null : cropClassItem.getName());
            }

            cropExcel.setDistrict(districtRepository.findByCode(cropLand.getDistrict().toString()).getName());

            if(cropLand.getGrowingPeriod() != null) {
                CropPeriod cropPeriod = cropPeriodService.findOne(cropLand.getGrowingPeriod());
                cropExcel.setGrowingPeriod(cropPeriod == null ? null : cropPeriod.getName());
            }
            cropExcel.setDesc(cropLand.getDesc());
            res.add(cropExcel);
        }
        return res;
    }


}
