/**
 * 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.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.nh.factory.goodwe.dto.getinventersmoredetailinfobysn.D;
import org.springblade.modules.order.constant.OrderConstants;
import org.springblade.modules.order.pojo.entity.OperationOrderCallbackEntity;
import org.springblade.modules.order.pojo.entity.OperationOrderEntity;
import org.springblade.modules.order.pojo.entity.OperationOrderOperationInfoEntity;
import org.springblade.modules.order.pojo.vo.OperationOrderCallbackVO;
import org.springblade.modules.order.excel.OperationOrderCallbackExcel;
import org.springblade.modules.order.mapper.OperationOrderCallbackMapper;
import org.springblade.modules.order.pojo.vo.SelectVo;
import org.springblade.modules.order.service.IOperationOrderCallbackService;
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 BladeX
 * @since 2024-06-21
 */
@Service
public class OperationOrderCallbackServiceImpl extends ServiceImpl<OperationOrderCallbackMapper, OperationOrderCallbackEntity> implements IOperationOrderCallbackService {

	Logger logger= LoggerFactory.getLogger(getClass());

	@Autowired
	OperationOrderServiceImpl orderService;

	@Autowired
	OperationOrderOperationInfoServiceImpl infoService;

	//工单回访
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean addOrderCllBack(OperationOrderCallbackEntity entity) {
        logger.info("addOrderCllBack  orderId:{},result:{}",entity.getOrderId(),entity.getServiceAttitude());

		OperationOrderEntity order = orderService.getById(entity.getOrderId());
		if(!OrderConstants.ORDER_STATUS_CALL.equals(order.getStatus()) && !OrderConstants.ORDER_STATUS_SETTLE.equals(order.getStatus())){
			throw new ServiceException("工单状态不支持回访操作,请刷新!");
		}
		if(OrderConstants.RESULT_OK.equals(order.getCallbackResult())){
			throw new ServiceException("工单已回访，请勿在次回访!");
		}
		BladeUser user = AuthUtil.getUser();
		Date date=new Date();
		order.setCallbackResult(OrderConstants.RESULT_OK);
		order.setLatestProcess(user.getNickName() + "工单回访");
		order.setLatestProcessTime(date);
		Boolean flag1= orderService.updateById(order);
		if(!flag1){
			throw new ServiceException("修改工单表失败!");
		}
		//更新过程信息
		Target ta = new Target();
		ta.setD(DateUtil.formatDateTime(date));
		ta.setP(user.getNickName());
		ta.setC(user.getNickName() + "工单回访");
		OperationOrderOperationInfoEntity info = infoService.findInfoByOrderId(order.getId());
		ta.setT(Target.MESS_CALLBACK);
		if(StringUtil.isBlank(info.getId())){
			info.setOrderId(order.getId());
		}
		String str = WebPageFunUtils.appendOrderProcessDetail(ta, info.getProcessDetail());
		info.setProcessDetail(str);
		if(!infoService.saveOrUpdate(info)){
			throw new ServiceException("保存过程信息失败!");
		}
		entity.setCreateName(user.getNickName());
		entity.setCreateBy(user.getUserId().toString());
		entity.setCreateTime(date);
		return save(entity);
	}

	@Override
	public List<SelectVo> getAllBrand() {
		List<SelectVo> list=baseMapper.getAllBrand();
		return list;
	}

}
