package org.springblade.business.service.impl;


import lombok.AllArgsConstructor;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.*;
import org.springblade.business.entity.MileagePart;
import org.springblade.business.entity.Mileagexy;
import org.springblade.business.mapper.MileagexyMapper;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class MileagexyService  extends BaseServiceImpl<MileagexyMapper, Mileagexy> {

    private  final MileagePartService partService;


//    public R<IPage<Mileagexy>> list(Map<String, Object> MapParam, Integer start, Integer rows) {
//        SmartResponse<Object> smartResp = new SmartResponse<Object>();
//        smartResp.setResult(OP_NOT_DATA_SUCCESS);
//        smartResp.setMsg(OP_NOT_DATA_SUCCESS_MSG);
//        String sql =SQL_MLIST;
//        try {
//            if(StringUtils.isNotEmpty(sql)) {
//                long totalNum = getOPDao().count(sql,MapParam);
//                if(start<=totalNum) {
//                    List<Object> objs = getOPDao().queryMapDatas(sql,MapParam, start, rows);
//                    if(null != objs && objs.size()>0) {
//                        smartResp.setResult(OP_SUCCESS);
//                        smartResp.setMsg(OP_SUCCESS_MSG);
//                        smartResp.setDatas(objs);
//                        smartResp.setPerPageSize(rows);
//                        smartResp.setTotalNum(totalNum);
//                        smartResp.setTotalPage(PageHelper.getTotalPage(totalNum, rows));
//                        smartResp.setSize(objs.size());
//                    }
//                }
//            }
//        } catch (DaoException e){
//            e.printStackTrace();
//        } catch (Exception e){
//            e.printStackTrace();
//        }
//        return smartResp;
//    }
//


//    public List<Mileagexy> getlist(String partId,Integer deleted,String projId) {
////        String sql = "select * from t_om_mileagexy  where partId = :partId  and  deleted=:deleted and projId=:projId order by orderNum ASC";
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("partId",partId);
//        map.put("deleted",deleted);
//        map.put("projId",projId);
//        List<Mileagexy> result = this.mileagexyDao.queryByField(map,"orderNum ASC");
////        List<Mileagexy> result = getDao().querySqlToEntity(sql, map,Mileagexy.class);
//        return result;
//    }


    public Integer toType(String value){
        String[] keys={"直线段","圆曲线","入缓和曲线","出缓和曲线","不完整缓和曲线"};
        for(int i=0;i<keys.length;i++){
            if(value.equals(keys[i])){
                return i;
            }
        }
        return 0;
    }
    public static Object getValue(Cell cell) {
        if(cell != null) {
            switch(cell.getCellTypeEnum()){
                case STRING:
                    return cell.getStringCellValue()==null ? null : cell.getStringCellValue().trim();
                case NUMERIC:
                    HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
                    return dataFormatter.formatCellValue(cell);
                case BOOLEAN:
                    return cell.getBooleanCellValue();
                case ERROR:
                    return cell.getErrorCellValue();
                case FORMULA:
                    try {
                        return cell.getStringCellValue();
                    } catch (IllegalStateException e) {
                        return cell.getNumericCellValue();
                    }
                default:
                    cell.setCellType(CellType.STRING);
                    return cell.getStringCellValue()==null ? null : cell.getStringCellValue().trim();
            }
        }
        return null;
    }


    public String importMileagexy(MultipartFile file,Long userId,String projectId, String contractId ,String partId) throws Exception {
        try {
            Workbook wb = WorkbookFactory.create(file.getInputStream());
            Sheet sheet = wb.getSheetAt(0);
            List<Mileagexy> importList = new ArrayList<>();
            MileagePart part = this.partService.getById(partId);
            if(part==null){
                return "所属片段不存在，请重新再试";
            }
            for (int i = 1; ; i++) {
                Row row = sheet.getRow(i);
                if(row == null) {
                    break;
                }
                Mileagexy obj = new Mileagexy();
                for(int j=0;j<14;j++){
                    Object v=getValue(row.getCell(j));
                    String value="0";
                    if(v!=null){
                         value = v.toString();
                    }
                    switch (j){
                        case 0:obj.setType(toType(value));break;
                        case 1:obj.setValue(Double.valueOf(value));break;
                        case 2:obj.setX(Double.valueOf(value));break;
                        case 3:obj.setY(Double.valueOf(value));break;
                        case 4:obj.setR(Double.valueOf(value));break;
                        case 5:if("无".equals(value)||"0".equals(value)){obj.setSwing("");}else{obj.setSwing((String) value);};break;
                        case 6:obj.setL(Double.valueOf(value));break;
                        case 7:obj.setDu(Integer.valueOf(value));break;
                        case 8:obj.setFen(Integer.valueOf(value));break;
                        case 9:obj.setMiao(Double.valueOf(value));break;
                        case 10:obj.setDlq(Double.valueOf(value));break;
                        case 11:obj.setDlh(Double.valueOf(value));break;
                        case 12:obj.setR2(Double.valueOf(value));break;
                        case 13:obj.setOrderNum(Integer.valueOf(value));break;
                        default:break;
                    }

                }
                obj.setUpdateUser(userId);
                obj.setProjectId(projectId);
                obj.setValueEnd(obj.getValue()+obj.getL());
                obj.setPartId(partId);
                obj.setContractId(contractId);
                obj.setPartNo(part.getPrefix());
                //必须要设置angle
                obj.setAngle(obj.getDu()+Double.valueOf(obj.getFen())/60+obj.getMiao()/3600);
                importList.add(obj);
            }
            this.saveBatch(importList);
              return  "导入成功";
        } catch (Exception e){
            e.printStackTrace();
        }
        return "导入失败";
    }


    public Mileagexy getMaxMileagexy(String partId,Integer deleted,String projId,Integer orderNum){
        if(null!=orderNum&&orderNum>0){
            orderNum--;
        }
        List<Mileagexy> result = this.baseMapper.getLast(partId,projId,orderNum);
        if(result.size()>0){
            return result.get(0);
        }else{
            return null;
        }
    }

}
