package com.tanpu.feo.feojob.service;

import com.tanpu.feo.feojob.constant.BaseConstant;
import com.tanpu.feo.feojob.dao.diagnose.entity.IfaAssetsEntity;
import com.tanpu.feo.feojob.dao.user.entity.*;
import com.tanpu.feo.feojob.dto.WorkDataDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OrgSyncByWxcpService {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private DepartmentEmployeeService departmentEmployeeService;
    @Resource
    private EmployeeService employeeService;
    @Resource
    private DepartmentService departmentService;
    @Resource
    private EmployeeRoleService employeeRoleService;


    @Transactional
    public List<IfaAssetsEntity> execute(WorkDataDto<UserInfoEntity> workUserInfo, WorkDataDto<EmployeeEntity> workEmployee, WorkDataDto<DepartmentEntity> workDepartment,
                                         WorkDataDto<DepartmentEmployeeEntity> workDepartmentEmployee, WorkDataDto<EmployeeRoleEntity> workEmployeeRole) {
        ArrayList<IfaAssetsEntity> assetsArrayList = new ArrayList<>();
        Date now = new Date();
        log.info("==========开始 执行数据修改==========");
        // 1
        List<UserInfoEntity> workUserInfoInsertList = workUserInfo.getInsertList();
        for (UserInfoEntity userInfoEntity : workUserInfoInsertList) {
            userInfoEntity.setCreateby(BaseConstant.SYS);
            userInfoEntity.setCreatetime(now);
            userInfoEntity.setUpdateby(BaseConstant.SYS);
            userInfoEntity.setUpdatetime(now);
            userInfoEntity.setDeletetag(BaseConstant.DeleteTagStr.NOT_DELETED);
        }
        userInfoService.saveBatch(workUserInfoInsertList);

        List<UserInfoEntity> workUserInfoUpdateList = workUserInfo.getUpdateList();
        for (UserInfoEntity userInfoEntity : workUserInfoUpdateList) {
            userInfoEntity.setUpdateby(BaseConstant.SYS);
            userInfoEntity.setUpdatetime(now);
            userInfoEntity.setDeletetag(BaseConstant.DeleteTagStr.NOT_DELETED);
        }
        userInfoService.updateBatchById(workUserInfoUpdateList);

        List<UserInfoEntity> workUserInfoDeleteList = workUserInfo.getDeleteList();
        for (UserInfoEntity userInfoEntity : workUserInfoDeleteList) {
            userInfoEntity.setUpdateby(BaseConstant.SYS);
            userInfoEntity.setUpdatetime(now);
            userInfoEntity.setDeletetag(BaseConstant.DeleteTagStr.DELETED);
        }
        userInfoService.updateBatchById(workUserInfoDeleteList);

        //2
        List<EmployeeEntity> workEmployeeInsertList = workEmployee.getInsertList();
        for (EmployeeEntity employeeEntity : workEmployeeInsertList) {
            employeeEntity.setCreateBy(BaseConstant.SYS);
            employeeEntity.setCreateTime(now);
            employeeEntity.setUpdateBy(BaseConstant.SYS);
            employeeEntity.setUpdateTime(now);
            employeeEntity.setDeleteTag(BaseConstant.DeleteTagStr.NOT_DELETED);
            IfaAssetsEntity ifaAssetsEntity = new IfaAssetsEntity();
            ifaAssetsEntity.setIfaId(employeeEntity.getEmployeeId());
            ifaAssetsEntity.setId(employeeEntity.getEmployeeId());
            ifaAssetsEntity.setCreateTime(now);
            ifaAssetsEntity.setCreateBy(BaseConstant.SYS);
            ifaAssetsEntity.setUpdateTime(now);
            ifaAssetsEntity.setUpdateBy(BaseConstant.SYS);
            ifaAssetsEntity.setDeleteTag(BaseConstant.DeleteTagStr.NOT_DELETED);
            assetsArrayList.add(ifaAssetsEntity);
        }
        employeeService.saveBatch(workEmployeeInsertList);


        List<EmployeeEntity> workEmployeeDeleteList = workEmployee.getDeleteList();
        for (EmployeeEntity employeeEntity : workEmployeeDeleteList) {
            employeeEntity.setUpdateBy(BaseConstant.SYS);
            employeeEntity.setUpdateTime(now);
            employeeEntity.setDeleteTag(BaseConstant.DeleteTagStr.DELETED);
        }
        employeeService.updateBatchById(workEmployeeDeleteList);

        List<EmployeeEntity> workEmployeeUpdateList = workEmployee.getUpdateList();
        for (EmployeeEntity employeeEntity : workEmployeeUpdateList) {
            employeeEntity.setUpdateBy(BaseConstant.SYS);
            employeeEntity.setUpdateTime(now);
            employeeEntity.setDeleteTag(BaseConstant.DeleteTagStr.NOT_DELETED);
        }
        employeeService.updateBatchById(workEmployeeUpdateList);

        //3
        List<DepartmentEntity> workDepartmentInsertList = workDepartment.getInsertList();
        for (DepartmentEntity departmentEntity : workDepartmentInsertList) {
            departmentEntity.setCreateBy(BaseConstant.SYS);
            departmentEntity.setCreateTime(now);
            departmentEntity.setUpdateBy(BaseConstant.SYS);
            departmentEntity.setUpdateTime(now);
            departmentEntity.setDeleteTag(BaseConstant.DeleteTagStr.NOT_DELETED);
        }
        departmentService.saveBatch(workDepartmentInsertList);

        List<DepartmentEntity> workDepartmentDeleteList = workDepartment.getDeleteList();
        for (DepartmentEntity departmentEntity : workDepartmentDeleteList) {
            departmentEntity.setUpdateBy(BaseConstant.SYS);
            departmentEntity.setUpdateTime(now);
            departmentEntity.setDeleteTag(BaseConstant.DeleteTagStr.DELETED);
        }
        departmentService.updateBatchById(workDepartmentDeleteList);

        List<DepartmentEntity> workDepartmentUpdateList = workDepartment.getUpdateList();
        for (DepartmentEntity departmentEntity : workDepartmentUpdateList) {
            departmentEntity.setUpdateBy(BaseConstant.SYS);
            departmentEntity.setUpdateTime(now);
            departmentEntity.setDeleteTag(BaseConstant.DeleteTagStr.NOT_DELETED);
        }
        departmentService.updateBatchById(workDepartmentUpdateList);

        //4
        List<DepartmentEmployeeEntity> workDepartmentEmployeeInsertList = workDepartmentEmployee.getInsertList();
        for (DepartmentEmployeeEntity departmentEmployeeEntity : workDepartmentEmployeeInsertList) {
            departmentEmployeeEntity.setCreateBy(BaseConstant.SYS);
            departmentEmployeeEntity.setCreateTime(now);
            departmentEmployeeEntity.setUpdateBy(BaseConstant.SYS);
            departmentEmployeeEntity.setUpdateTime(now);
            departmentEmployeeEntity.setDeleteTag(BaseConstant.DeleteTagStr.NOT_DELETED);
        }
        departmentEmployeeService.saveBatch(workDepartmentEmployeeInsertList);

        List<DepartmentEmployeeEntity> workDepartmentEmployeeDeleteList = workDepartmentEmployee.getDeleteList();
        for (DepartmentEmployeeEntity departmentEmployeeEntity : workDepartmentEmployeeDeleteList) {
            departmentEmployeeEntity.setUpdateBy(BaseConstant.SYS);
            departmentEmployeeEntity.setUpdateTime(now);
            departmentEmployeeEntity.setDeleteTag(BaseConstant.DeleteTagStr.DELETED);
        }
        departmentEmployeeService.updateBatchById(workDepartmentEmployeeDeleteList);

        List<DepartmentEmployeeEntity> workDepartmentEmployeeUpdateList = workDepartmentEmployee.getUpdateList();
        for (DepartmentEmployeeEntity departmentEmployeeEntity : workDepartmentEmployeeUpdateList) {
            departmentEmployeeEntity.setUpdateBy(BaseConstant.SYS);
            departmentEmployeeEntity.setUpdateTime(now);
            departmentEmployeeEntity.setDeleteTag(BaseConstant.DeleteTagStr.NOT_DELETED);
        }
        departmentEmployeeService.updateBatchById(workDepartmentEmployeeUpdateList);

        //5
        List<EmployeeRoleEntity> workEmployeeRoleInsertList = workEmployeeRole.getInsertList();
        for (EmployeeRoleEntity employeeRoleEntity : workEmployeeRoleInsertList) {
            employeeRoleEntity.setCreateBy(BaseConstant.SYS);
            employeeRoleEntity.setCreateTime(now);
            employeeRoleEntity.setUpdateBy(BaseConstant.SYS);
            employeeRoleEntity.setUpdateTime(now);
            employeeRoleEntity.setDeleteTag(BaseConstant.DeleteTagStr.NOT_DELETED);
        }
        employeeRoleService.saveBatch(workEmployeeRoleInsertList);

        List<EmployeeRoleEntity> workEmployeeRoleDeleteList = workEmployeeRole.getDeleteList();
        for (EmployeeRoleEntity employeeRoleEntity : workEmployeeRoleDeleteList) {
            employeeRoleEntity.setUpdateBy(BaseConstant.SYS);
            employeeRoleEntity.setUpdateTime(now);
            employeeRoleEntity.setDeleteTag(BaseConstant.DeleteTagStr.DELETED);
        }
        employeeRoleService.updateBatchById(workEmployeeRoleDeleteList);

        List<EmployeeRoleEntity> workEmployeeRoleUpdateList = workEmployeeRole.getUpdateList();
        for (EmployeeRoleEntity employeeRoleEntity : workEmployeeRoleUpdateList) {
            employeeRoleEntity.setUpdateBy(BaseConstant.SYS);
            employeeRoleEntity.setUpdateTime(now);
            employeeRoleEntity.setDeleteTag(BaseConstant.DeleteTagStr.NOT_DELETED);
        }
        employeeRoleService.updateBatchById(workEmployeeRoleUpdateList);
        log.info("==========结束 执行数据修改==========");
        return assetsArrayList;
    }

}