package vip.xiaonuo.importData.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.hr.modular.basedepartment.entity.HrBaseDepartment;
import vip.xiaonuo.hr.modular.basedepartment.service.HrBaseDepartmentService;
import vip.xiaonuo.hr.modular.baseemployeeinfo.entity.HrBaseEmployeeInfo;
import vip.xiaonuo.hr.modular.baseemployeeinfo.service.HrBaseEmployeeInfoService;
import vip.xiaonuo.importData.entity.HrData;
import vip.xiaonuo.importData.service.ImportData;
import vip.xiaonuo.importData.utils.ExcelImportSheet;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ImportDataImpl implements ImportData {

    @Autowired
    private HrBaseDepartmentService departmentService;
    @Autowired
    private HrBaseEmployeeInfoService infoService;

    @Override
    @Transactional
    public CommonResult importData(HttpServletRequest httpServletRequest, String orgId, MultipartFile files) {
        Map<String , String> map = new HashMap<>();
        //表头与键值对的映射关系
        map.put("部门" , "departmentName");
        map.put("岗位" , "post");
        map.put("序号" , "number");
        map.put("姓名" , "name");
        map.put("性别" , "gender");
        map.put("政治面貌" , "politicalOutlook");
        map.put("民族" , "nation");
        map.put("户口性质" , "hukouNature");
        map.put("出生年月" , "birthday");
        map.put("年龄(周岁)" , "age");
        map.put("身份证号码" , "idCardNumber");
        map.put("招入集团时间" , "joinGroup");
        map.put("入职时间" , "entryDate");
        map.put("工龄" , "seniority");
        map.put("合同起止时间" , "contactStartTime");
        map.put("文化程度" , "cultureLevel");
        map.put("专业" , "major");
        map.put("毕业院校" , "almaMater");
        map.put("职称" , "title");
        map.put("现任职务或岗位" , "current");
        map.put("新级别" , "newLevel");
        map.put("新薪档" , "newSalary");
        map.put("转正日期" , "confirmationTime");
        map.put("定额工资" , "quotaWages");
        map.put("籍贯" , "nativePlace");
        map.put("家庭地址或通讯地址" , "homeAddress");
        map.put("手机" , "phone");
        map.put("紧急联系人姓名" , "emergencyContactName");
        map.put("紧急联系人电话" , "emergencyContactPhone");
        map.put("工资发放处" , "payroll");
        map.put("社保" , "socialSecurity");
        map.put("公积金" , "providentFund");
        map.put("婚姻" , "marriage");

        try(
                //这里面的对象会自动关闭
                InputStream in = files.getInputStream();
                Workbook workbook = ExcelImportSheet.getTypeFromExtends(in , files.getOriginalFilename())
        ) {

            //根据名称获取单张表对象 也可以使用getSheetAt(int index)获取单张表的对象 获取第一张表
            Sheet sheet = workbook.getSheetAt(0);
            List<HrData> list = ExcelImportSheet.getListFromExcel(sheet , HrData.class , map);

            for (HrData hrData : list) {
                String department = hrData.getDepartmentName();
                System.out.println("这个是部门的名称："+department);
                if ("".equals(department) || null == department) {
                } else {
                    HrBaseDepartment departments = departmentService.getOne(new QueryWrapper<HrBaseDepartment>().eq("name", department).eq("org_id",orgId));
                    if (ObjectUtil.isNotEmpty(departments)) {
                    } else {
                        HrBaseDepartment department1 = new HrBaseDepartment();
                        department1.setOrgId(orgId);
                        department1.setName(department);
                        department1.setCreateTime(new Date());
                        departmentService.save(department1);
                    }
                }
            }
            List<HrBaseDepartment> departments1 = departmentService.list(new QueryWrapper<HrBaseDepartment>().eq("org_id", orgId));

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            for (HrData hrData : list) {
                HrBaseEmployeeInfo info = new HrBaseEmployeeInfo();
                BeanUtil.copyProperties(hrData, info);
              /*  if ("".equals(hrData.getBirthday1()) || null == hrData.getBirthday1()) {
                } else {
                    Date parse = sdf.parse(hrData.getBirthday1());
                    info.setBirthday(parse);
                }
                if ("".equals(hrData.getJoinGroup1()) || null == hrData.getJoinGroup1()) {
                } else {
                    Date parse = sdf.parse(hrData.getJoinGroup1());
                    info.setJoinGroup(parse);
                }
                if ("".equals(hrData.getEntryDate1()) || null == hrData.getEntryDate1()) {
                } else {
                    Date parse = sdf.parse(hrData.getEntryDate1());
                    info.setEntryDate(parse);
                }
                if ("".equals(hrData.getContactStartTime1()) || null == hrData.getContactStartTime1()) {
                } else {
                    Date parse = sdf.parse(hrData.getContactStartTime1());
                    info.setContactStartTime(parse);
                }
                if ("".equals(hrData.getConfirmationTime1()) || null == hrData.getConfirmationTime1()) {
                } else {
                    Date parse = sdf.parse(hrData.getConfirmationTime1());
                    info.setConfirmationTime(parse);
                }*/
                info.setOrgId(orgId);
                for (HrBaseDepartment department : departments1) {
                    if (department.getName().equals(hrData.getDepartmentName())) {
                        info.setDepartmentName(department.getName());
                        info.setDepartmentId(department.getId().toString());
                    }
                }
                if ("男".equals(hrData.getGender()) || "男性".equals(hrData.getGender())) {
                    info.setGender("0");
                }
                if ("女".equals(hrData.getGender()) || "女性".equals(hrData.getGender())) {
                    info.setGender("1");
                }
                if ("已婚".equals(hrData.getMarriage())) {
                    info.setMarriage("1");
                }
                if ("未婚".equals(hrData.getMarriage())) {
                    info.setMarriage("0");
                }
                if ("小学".equals(hrData.getCultureLevel())) {
                    info.setEducation(0);
                }
                if ("初中".equals(hrData.getCultureLevel())) {
                    info.setEducation(1);
                }
                if ("高中".equals(hrData.getCultureLevel())) {
                    info.setEducation(2);
                }
                if ("本科".equals(hrData.getCultureLevel())) {
                    info.setEducation(3);
                }
                if ("研究生".equals(hrData.getCultureLevel())|| "硕士".equals(hrData.getCultureLevel())) {
                    info.setEducation(4);
                }
                if ("博士".equals(hrData.getCultureLevel())|| "博士研究生".equals(hrData.getCultureLevel())) {
                    info.setEducation(5);
                }

                if ("".equals(hrData.getIdCardNumber()) || null == hrData.getIdCardNumber()) {
                    return CommonResult.error("表格中存在身份证号为空！请检查后重新导入");
                } else {
                    HrBaseEmployeeInfo infos = infoService.getOne(new QueryWrapper<HrBaseEmployeeInfo>().eq("ID_CARD_NUMBER", hrData.getIdCardNumber()));
                    if (ObjectUtil.isEmpty(infos)) {
                        infoService.save(info);
                    } else {
                        info.setId(infos.getId());
                        infoService.updateById(info);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error();
        }finally {
            //写着好看的
        }
        return CommonResult.ok();
    }
}
