package com.yihu.jw.doctor.service.excel;

import com.yihu.jw.util.excel.poi.AExcelReader;
import com.yihu.jw.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by Trick on 2020/3/26.
 */
@Component
public class DoctorWorkTimeExcelReader extends AExcelReader {


    @Override
    public void read(Workbook rwb) throws Exception {
        try{
            Iterator<Sheet> sheets = rwb.sheetIterator();
            int j = 0, rows;

            while (sheets.hasNext()){
                Sheet sheet = sheets.next();

                if(sheet.getLastRowNum()>202){
                    setCode("-1");
                    setMes("目前仅支持200位医生同时导入");
                    return;
                }

                if ((rows = sheet.getLastRowNum()) == 0) {
                    continue;
                }
                for (int i = 3; i <= rows; i++) {
                    //1.获取医生信息
                    String idCard = replaceBlank(getCellCont(sheet, i, 0));
                    String name = replaceBlank(getCellCont(sheet, i, 1));

                    DoctorWorkTimeMainExcelDO main = new DoctorWorkTimeMainExcelDO();
                    main.setName(name);
                    main.setIdCard(idCard);
                    main.setRow(i);
                    List<DoctorWorkTimeExcelDO> list = new ArrayList<>();

                    //2.构造每天排班记录
                    //遍历上午排班信息
                    int sw = 2;
                    while (sw<=92){
//                        System.out.println("col:"+sw);
//                        System.out.println("day:"+(sw+1)/3);
                        String workflag =  replaceBlank(getCellCont(sheet,i,sw));
                        //判断是否有排班
                        if("1".equals(workflag)){
                            //如果有排班，生成当天排班记录
                            DoctorWorkTimeExcelDO excelDO = new DoctorWorkTimeExcelDO();
                            excelDO.setName(name);
                            excelDO.setIdCard(idCard);
                            //标记行列
                            excelDO.setRow(i);
                            excelDO.setCol(sw);

                            Integer day = (sw+1)/3;
                            excelDO.setDateOfMonth(day+"");
                            excelDO.setType("1");
                            list.add(excelDO);

                        }
                        sw = sw+3;
                    }

                    //遍历下午排班信息
                    int xw = 3;
                    while (xw<=93){
                        //
//                        System.out.println("col:"+xw);
//                        System.out.println("day:"+(xw)/3);

                        String workflag =  replaceBlank(getCellCont(sheet,i,xw));
                        //判断是否有排班
                        if("1".equals(workflag)){
                            //如果有排班，生成当天排班记录
                            DoctorWorkTimeExcelDO excelDO = new DoctorWorkTimeExcelDO();
                            excelDO.setName(name);
                            excelDO.setIdCard(idCard);
                            //标记行列
                            excelDO.setRow(i);
                            excelDO.setCol(xw);

                            Integer day = (xw+1)/3;
                            excelDO.setDateOfMonth(day+"");
                            excelDO.setType("2");
                            list.add(excelDO);

                        }
                        xw = xw+3;
                    }

                    //遍历晚上排班信息
                    int ws = 4;
                    while (ws<=94){
                        //
//                        System.out.println("col:"+ws);
//                        System.out.println("day:"+(ws-1)/3);
                        String workflag =  replaceBlank(getCellCont(sheet,i,ws));
                        //判断是否有排班
                        if("1".equals(workflag)){
                            //如果有排班，生成当天排班记录
                            DoctorWorkTimeExcelDO excelDO = new DoctorWorkTimeExcelDO();
                            excelDO.setName(name);
                            excelDO.setIdCard(idCard);
                            //标记行列
                            excelDO.setRow(i);
                            excelDO.setCol(ws);

                            Integer day = (ws+1)/3;
                            excelDO.setDateOfMonth(day+"");
                            excelDO.setType("3");
                            list.add(excelDO);

                        }
                        ws = ws+3;
                    }
                    //设置本月排班
                    main.setWorkTime(list);

                    String rs = checkMain(main);
                    if("1".equals(rs)){
                        getCorrectLs().add(main);
                    }else {
                        getErrorLs().add(main);
                    }
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }finally {
            if (rwb != null) {
                rwb.close();
            }
        }
    }

    public String checkMain(DoctorWorkTimeMainExcelDO main){
        String idCard = main.getIdCard();
        String name = main.getName();
        if(StringUtils.isBlank(idCard)){
            main.setErrorMes("第"+(main.getRow()+1)+"行,身份证号为空！\n");
            return "0";
        }
        if(StringUtils.isBlank(name)){
            main.setErrorMes("第"+(main.getRow()+1)+"行,姓名为空！\n");
            return "0";
        }
        return "1";
    }

//    public static void main(String ag[]){
//        int sw = 2;
//        while (sw<=92){
//            //
//            System.out.println("col:"+sw);
//            System.out.println("day:"+(sw+1)/3);
//            sw = sw+3;
//        }
//
//        int xw = 3;
//        while (xw<=93){
//            //
//            System.out.println("col:"+xw);
//            System.out.println("day:"+(xw)/3);
//            xw = xw+3;
//        }
//
//        int ws = 4;
//        while (ws<=94){
//            //
//            System.out.println("col:"+ws);
//            System.out.println("day:"+(ws-1)/3);
//            ws = ws+3;
//        }
//    }
}
