/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.order.constant.OrderConstants;
import org.springblade.modules.order.excel.OperationOrderFeedbackExcel;
import org.springblade.modules.order.mapper.OperationOrderFeedbackMapper;
import org.springblade.modules.order.pojo.entity.OperationOrderDispatchEntity;
import org.springblade.modules.order.pojo.entity.OperationOrderEntity;
import org.springblade.modules.order.pojo.entity.OperationOrderFeedbackEntity;
import org.springblade.modules.order.pojo.entity.OperationOrderOperationInfoEntity;
import org.springblade.modules.order.pojo.vo.OperationOrderFeedbackVO;
import org.springblade.modules.order.service.IOperationOrderFeedbackService;
import org.springblade.modules.order.utils.Target;
import org.springblade.modules.order.utils.WebPageFunUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 工单反馈表 服务实现类
 *
 * @author lzp
 * @since 2024-06-21
 */
@Service
public class OperationOrderFeedbackServiceImpl extends ServiceImpl<OperationOrderFeedbackMapper, OperationOrderFeedbackEntity> implements IOperationOrderFeedbackService {

	@Autowired
	OperationOrderServiceImpl orderService;
	@Autowired
	OperationOrderOperationInfoServiceImpl infoService;
	@Autowired
	OperationOrderDispatchServiceImpl dispatchService;

	@Autowired
	OperationOrderSiteDispatchServiceImpl siteDispatchService;

	//工单反馈  过程反馈+服务完成
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean feedbackSealingSheet(OperationOrderFeedbackVO feedback) {
		BladeUser user = AuthUtil.getUser();
		Date time = new Date();
		OperationOrderEntity or = orderService.getById(feedback.getOrderId());
		if (StringUtil.isNotBlank(feedback.getApplianceBrand())) {
			or.setApplianceBrand(feedback.getApplianceBrand());
		}
		if (StringUtil.isNotBlank(feedback.getApplianceBarcode())) {
			or.setApplianceBarcode(feedback.getApplianceBarcode());
		}
		if (StringUtil.isNotBlank(feedback.getWarrantyType())) {
			or.setWarrantyType(feedback.getWarrantyType());
		}
		if (StringUtil.isNotBlank(feedback.getServiceType())) {
			or.setServiceType(feedback.getServiceType());
		}
		/*if (feedback.getPutIntoTime() != null) {
			or.setPutIntoTime(feedback.getPutIntoTime());
		}*/
		if (StringUtil.isNotBlank(feedback.getServiceMeasures())) {
			or.setServiceMeasures(feedback.getServiceMeasures());
		}
		String strr=StringUtil.isBlank(feedback.getFeedback())?"":",反馈内容:"+feedback.getFeedback();
		if ("1".equals(feedback.getFeedbackType())) {   //0 过程反馈 1服务完成反馈
			if (StringUtil.isBlank(feedback.getApplianceBrand()) || StringUtil.isBlank(feedback.getApplianceBarcode()) || StringUtil.isBlank(feedback.getWarrantyType()) || StringUtil.isBlank(feedback.getServiceMeasures())) {
				throw new ServiceException("必传项不能为空!");
			}
			//判断是否有审核中的物料申请
			long count=orderService.getBaseMapper().coutNoFinishFittingApply(or.getNumber());
			if(count>0){
				throw new ServiceException("该工单有未完成的物料申请，不能完工！");
			}
			if(StringUtil.isBlank(or.getServiceType())){
				throw new ServiceException("服务类型为空，请先维护服务类型");
			}
			if (!OrderConstants.SITE_STATUS_RECEIVE.equals(or.getSiteStatus()) && !OrderConstants.SITE_STATUS_DOOR.equals(or.getSiteStatus()) && !OrderConstants.SITE_STATUS_SERVICE.equals(or.getSiteStatus())) {
				throw new ServiceException("不满足反馈封单条件");
			}
			or.setLatestProcessTime(new Date());
			or.setLatestProcess(user.getNickName() + "反馈封单,反馈内容:" + feedback.getFeedback());
			if (or.getDropInTime() == null) {
				or.setDropInTime(time);
			}
			or.setEndTime(time);
			or.setStatus(OrderConstants.ORDER_STATUS_SETTLE);
			or.setSiteStatus(OrderConstants.SITE_STATUS_FINISH);
			orderService.updateById(or);

			Target ta = new Target();
			ta.setD(DateUtil.formatDateTime(time));
			ta.setP(user.getNickName());

			ta.setC(user.getNickName() + "服务完工" + strr);
			OperationOrderOperationInfoEntity info = infoService.findInfoByOrderId(feedback.getOrderId());
			ta.setT(Target.MESS_CALLBACK_TWICE);
			String str = WebPageFunUtils.appendOrderProcessDetail(ta, info.getProcessDetail());
			info.setProcessDetail(str);
			infoService.updateById(info);

			OperationOrderDispatchEntity dis = dispatchService.findOrderDisByOrderId(feedback.getOrderId());
			if(dis!= null){
				dis.setStatus(OrderConstants.DISPATCH_STATUS_FINISH);
				dis.setUpdateBy(user.getUserId().toString());
				dis.setUpdateTime(time);
				dis.setEndTime(time);
				if (dis.getDropInTime() == null) {
					dis.setDropInTime(time);
				}
				dispatchService.updateById(dis);
			}
		} else {

			if(StringUtil.isBlank(feedback.getFeedback()) || StringUtil.isBlank(feedback.getFeedbackImg())){
				throw new ServiceException("反馈信息不能为空！");
			}
			Target ta = new Target();
			ta.setD(DateUtil.formatDateTime(time));
			ta.setP(user.getNickName());
			ta.setC(user.getNickName() + "过程反馈" + strr);
			OperationOrderOperationInfoEntity info = infoService.findInfoByOrderId(feedback.getOrderId());
			ta.setT(Target.MESS_CALLBACK_TWICE);
			String str = WebPageFunUtils.appendOrderProcessDetail(ta, info.getProcessDetail());
			info.setProcessDetail(str);
			infoService.updateById(info);

			or.setLatestProcessTime(time);
			or.setLatestProcess(user.getNickName() + "过程反馈" + strr);
			orderService.updateById(or);
		}

		feedback.setFeedbackId(user.getUserId().toString());
		feedback.setFeedbackName(user.getNickName());
		feedback.setSiteId(or.getSiteId());
		feedback.setFeedbackTime(time);
		return save(feedback);
	}

	//工单的反馈信息
	@Override
	public List<OperationOrderFeedbackEntity> listFeedback(String orderId) {
		LambdaQueryWrapper<OperationOrderFeedbackEntity> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(OperationOrderFeedbackEntity::getOrderId, orderId);
		queryWrapper.orderByDesc(OperationOrderFeedbackEntity::getFeedbackTime);
		List<OperationOrderFeedbackEntity> list = list(queryWrapper);
		return list;
	}

}
