/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.business.check.order.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.cache.DictBizCache;
import org.springblade.constant.BusinessConstant;
import org.springblade.constant.DictBusinessConstant;
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.enums.OrderStatusEnum;
import org.springblade.enums.WarnKindEnum;
import org.springblade.modules.baseinfo.dict.entity.DictInfoEntity;
import org.springblade.modules.baseinfo.dict.service.IBaseInfoDictService;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.check.order.vo.CheckOrderInfoVO;
import org.springblade.modules.business.check.process.entity.OrderProcessInfoEntity;
import org.springblade.modules.business.check.process.service.IRtuOrderProcessService;
import org.springblade.modules.business.data.service.IRtuDataRainService;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.service.IRtuWarningService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.entity.Role;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.modules.system.service.IRoleService;
import org.springblade.modules.system.service.IUserService;

import java.time.Duration;
import java.util.*;

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

    private static IDictBizService dictBizService;

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

    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 IUserService userService;

    private static IUserService getUserService() {
        if (userService == null) {
            userService = SpringUtil.getBean(IUserService.class);
        }
        return userService;
    }

    private static IRoleService roleService;

    private static IRoleService getRoleService() {
        if (roleService == null) {
            roleService = SpringUtil.getBean(IRoleService.class);
        }
        return roleService;
    }

    private static IRtuWarningService rtuWarningService;

    private static IRtuWarningService getRtuWarningService() {
        if (rtuWarningService == null) {
            rtuWarningService = SpringUtil.getBean(IRtuWarningService.class);
        }
        return rtuWarningService;
    }


    private static IRtuOrderProcessService rtuOrderProcessService;

    private static IRtuOrderProcessService getRtuOrderProcessService() {
        if (rtuOrderProcessService == null) {
            rtuOrderProcessService = SpringUtil.getBean(IRtuOrderProcessService.class);
        }
        return rtuOrderProcessService;
    }

    @Override
    public CheckOrderInfoVO entityVO(CheckOrderInfoEntity entity) {
        CheckOrderInfoVO checkOrderInfoVO = Objects.requireNonNull(BeanUtil.copy(entity, CheckOrderInfoVO.class));
        return this.entityVO(checkOrderInfoVO);
    }

    public CheckOrderInfoVO entityVO(CheckOrderInfoVO vo) {
        if (Func.notNull(vo.getOrderProcessId())) {
            OrderProcessInfoEntity orderProcessInfoEntity = getRtuOrderProcessService().getById(vo.getOrderProcessId());
            User user = getUserService().getById(orderProcessInfoEntity.getCreateUser());
            vo.setProcessorName(user.getRealName());
            vo.setOrderProcessTime(orderProcessInfoEntity.getCreateTime());
            vo.setProcessDesc(orderProcessInfoEntity.getProcessDesc());
            vo.setCostsReportDesc(orderProcessInfoEntity.getCostsReportDesc());
        }
        if (Func.isNull(vo.getCreateOrderPersonName())) {
            User user = getUserService().getById(vo.getCreateUser());
            vo.setCreateOrderPersonName(user.getRealName());
        }

        DictBiz dictBiz = getDictBizService().getById(vo.getOrderStatusKey());
        if (Func.notNull(dictBiz)) {
            vo.setOrderStatusName(dictBiz.getDictValue());
        }

        if (Func.notNull(vo.getOrderPhotos())) {
            String[] photos = vo.getOrderPhotos().split(",");
            List<String> photoList = new ArrayList<>();
            for (String p : photos) {
                photoList.add(p);
            }
            vo.setOrderPhotoList(photoList);
        }

        if (Func.notNull(vo.getOrderVideos())) {
            String[] videos = vo.getOrderVideos().split(",");
            List<String> videoList = new ArrayList<>();
            for (String p : videos) {
                videoList.add(p);
            }
            vo.setOrderVideoList(videoList);
        }

        if (Func.notNull(vo.getOrderConfirmUser())){
            User user = getUserService().getById(vo.getOrderConfirmUser());
            vo.setOrderConfirmName(user.getRealName());
        }

        return vo;
    }

}
