/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.business.check.process.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.business.check.process.entity.OrderProcessInfoEntity;
import org.springblade.modules.business.check.process.vo.OrderProcessInfoVO;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;

import java.time.Duration;
import java.util.List;
import java.util.Objects;

/***
 * Date:2022/8/22
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
public class RtuOrderProcessWrapper extends BaseEntityWrapper<OrderProcessInfoEntity, OrderProcessInfoVO> {
	public static RtuOrderProcessWrapper build() {
		return new RtuOrderProcessWrapper();
	}

	private static IDeptRegionService deptRegionService;

	private static IDeptRegionService getDeptRegionService() {
		if (deptRegionService == null) {
			deptRegionService = SpringUtil.getBean(IDeptRegionService.class);
		}
		return deptRegionService;
	}

	private static IDeptService deptService;

	private static IDeptService getDeptService() {
		if (deptService == null) {
			deptService = SpringUtil.getBean(IDeptService.class);
		}
		return deptService;
	}

	private static IDictBizService dictBizService;

	private static IDictBizService getDictBizService() {
		if (dictBizService == null) {
			dictBizService = SpringUtil.getBean(IDictBizService.class);
		}
		return dictBizService;
	}


	@Override
	public OrderProcessInfoVO entityVO(OrderProcessInfoEntity entity) {
		OrderProcessInfoVO orderProcessInfoVO = Objects.requireNonNull(BeanUtil.copy(entity, OrderProcessInfoVO.class));
		return this.entityVO(orderProcessInfoVO);
	}

	public OrderProcessInfoVO entityVO(OrderProcessInfoVO orderProcessInfoVO) {
		if (null != orderProcessInfoVO.getOrderStatusKey()) {
			DictBiz biz =  getDictBizService().getById(orderProcessInfoVO.getOrderStatusKey());
			orderProcessInfoVO.setOrderStatusName(biz.getDictValue());
		}
		if (orderProcessInfoVO.getTaskCreateTime() != null) {
			Duration duration = Func.between(orderProcessInfoVO.getCreateTime(), orderProcessInfoVO.getTaskCreateTime());
			long second = duration.getSeconds();
			String taskTime = "";
			if (second >= 60 * 60) {
				if (second >= 24 * 60 * 60) {
					taskTime = "" + second / (24 * 60 * 60) + "天";
					second = second % (24 * 60 * 60);
				}
				if (second >= 60 * 60) {
					taskTime += "" + second / (60 * 60) + "小时";
					second = second % (60 * 60);
				}
				if (second >= 60) {
					taskTime += "" + second / (60) + "分";
				}
			} else {
				taskTime = "1小时内";
			}
			orderProcessInfoVO.setTaskDuration(taskTime);
		}

		String ancestors = orderProcessInfoVO.getAncestors();
		if (null != ancestors && ancestors.length() > 0) {
			String[] ancestorsList = ancestors.split(",");
			String adCode;
			if (ancestorsList.length >= 3) {
				adCode = ancestorsList[2];
			} else {
				adCode = orderProcessInfoVO.getAdCode();
			}
			if (null != adCode) {
				LambdaQueryWrapper<DeptRegionEntity> wrapper = Wrappers.<DeptRegionEntity>query().lambda();
				wrapper.eq(DeptRegionEntity::getIsDeleted, 0);
				wrapper.eq(DeptRegionEntity::getAdcd, adCode);
				List<DeptRegionEntity> deptRegionEntities = getDeptRegionService().list(wrapper);
				if (null != deptRegionEntities && deptRegionEntities.size() > 0) {
					String manageCompany = "";
					for (DeptRegionEntity deptRegionEntity : deptRegionEntities) {
						Dept dept = getDeptService().getById(deptRegionEntity.getDeptId());
						if (null != dept) {
							if (manageCompany.length() > 0) {
								manageCompany += " / ";
							}
							manageCompany += dept.getDeptName();
						}
					}
					orderProcessInfoVO.setManageCompany(manageCompany);
				}
			}
		}


		return orderProcessInfoVO;
	}
}
