package com.yifu.cloud.plus.v1.yifu.order.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.order.constants.OrderConstants;
import com.yifu.cloud.plus.v1.yifu.order.entity.TOrder;
import com.yifu.cloud.plus.v1.yifu.order.entity.TOrderEnclosure;
import com.yifu.cloud.plus.v1.yifu.order.entity.TOrderReply;
import com.yifu.cloud.plus.v1.yifu.order.mapper.TOrderMapper;
import com.yifu.cloud.plus.v1.yifu.order.service.TOrderEnclosureService;
import com.yifu.cloud.plus.v1.yifu.order.service.TOrderReplyService;
import com.yifu.cloud.plus.v1.yifu.order.service.TOrderService;
import com.yifu.cloud.plus.v1.yifu.order.util.ValidityUtil;
import com.yifu.cloud.plus.v1.yifu.order.vo.OrderDetailVO;
import com.yifu.cloud.plus.v1.yifu.order.vo.OrderListParam;
import com.yifu.cloud.plus.v1.yifu.order.vo.OrderListVO;
import com.yifu.cloud.plus.v1.yifu.order.vo.OrderReplyListVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author licancan
 * @description 针对表【t_order(订单表)】的数据库操作Service实现
 * @date 2022-08-26 17:22:18
 */
@Slf4j
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {
	@Resource
	private TOrderEnclosureService tOrderEnclosureService;
	@Resource
	private TOrderReplyService tOrderReplyService;
	@Resource
	private OSSUtil ossUtil;

	/**
	 * 订单列表分页查询
	 *
	 * @param page
	 * @param param
	 * @return {@link IPage <OrderListVO>}
	 * @author licancan
	 */
	@Override
	public IPage<OrderListVO> getOrderPageList(Page<OrderListParam> page, OrderListParam param) {
		IPage<OrderListVO> orderList = baseMapper.getOrderPageList(page,param);
		return orderList;
	}

	/**
	 * 订单列表不分页查询
	 *
	 * @param param
	 * @return {@link List<OrderListVO>}
	 * @author licancan
	 */
	@Override
	public List<OrderListVO> getOrderList(OrderListParam param) {
		List<OrderListVO> list;
		if (CollectionUtils.isNotEmpty(param.getIdList())){
			list = baseMapper.getOrderListBySelect(param.getIdList());
		}else {
			list = baseMapper.getOrderList(param);
		}
		return list;
	}

	/**
	 * 变更订单状态
	 *
	 * @param id
	 * @param status
	 * @return {@link R <String>}
	 * @author licancan
	 */
	@Override
	public R<String> handleOrder(String id, Integer status) {
		if (StringUtils.isEmpty(id)){
			return R.failed(OrderConstants.ORDER_ID_IS_EMPTY);
		}
		if (CommonConstants.ONE_INT != status && CommonConstants.TWO_INT != status){
			return R.failed(OrderConstants.STATUS_ERROR);
		}
		YifuUser user = SecurityUtils.getUser();
		TOrder byId = this.getById(id);
		if (!Optional.ofNullable(byId).isPresent()){
			return R.failed(OrderConstants.DATA_IS_EMPTY);
		}
		if (CommonConstants.TWO_INT == byId.getOrderStatus()){
			return R.failed(OrderConstants.ORDER_IS_HANDLED);
		}
		byId.setOrderStatus(status);
		byId.setUpdateBy(user.getId());
		byId.setUpdateName(user.getNickname());
		byId.setUpdateTime(LocalDateTime.now());
		this.updateById(byId);
		// todo 如果是办结完成同步给ekp
		return R.ok(OrderConstants.OPERATE_SUCCESS);
	}

	/**
	 * 通过id查询详情
	 *
	 * @param id
	 * @return {@link R<OrderDetailVO>}
	 * @author licancan
	 */
	@Override
	public R<OrderDetailVO> getOrderDetailById(String id) {
		if (StringUtils.isEmpty(id)){
			return R.failed(OrderConstants.ORDER_ID_IS_EMPTY);
		}
		OrderDetailVO orderDetailVO = this.baseMapper.getOrderDetailById(id);
		//处理订单附件
		if (Optional.ofNullable(orderDetailVO).isPresent()){
			List<TOrderEnclosure> list = tOrderEnclosureService.list(Wrappers.<TOrderEnclosure>query().lambda()
					.eq(TOrderEnclosure::getOrderNo, orderDetailVO.getOrderNo())
					.eq(TOrderEnclosure::getEnclosureFlag,CommonConstants.ZERO_INT)
					.eq(TOrderEnclosure::getDeleteFlag,CommonConstants.ZERO_INT)
			);
			if (CollectionUtils.isNotEmpty(list)){
				orderDetailVO.setOrderEnclosure(list);
			}else {
				orderDetailVO.setOrderEnclosure(Lists.newArrayList());
			}
		}
		return R.ok(orderDetailVO);
	}

	/**
	 * 附件预览下载地址
	 *
	 * @param id 附件表id
	 * @return {@link R}
	 * @author licancan
	 */
	@Override
	public R getEnclosureUrl(String id) {
		if(StringUtils.isEmpty(id)){
			return R.failed(OrderConstants.ENCLOSURE_ID_IS_EMPTY);
		}
		TOrderEnclosure orderEnclosure = tOrderEnclosureService.getById(id);
		if (!Optional.ofNullable(orderEnclosure).isPresent()) {
			return R.failed(OrderConstants.DATA_IS_EMPTY);
		}
		String enclosureAddress = orderEnclosure.getEnclosureAddress();
		log.info("key:"+enclosureAddress);
		URL url = ossUtil.getObjectUrl(null, orderEnclosure.getEnclosureAddress());
		return new R<>(url.toString());
	}

	/**
	 * 添加回复
	 *
	 * @author licancan
	 * @param file         附件
	 * @param filePath     附件路径
	 * @param replyContent 回复内容
	 * @param orderNo      订单编号
	 * @return {@link R}
	 * @throws IOException
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public R uploadReplyEnclosure(MultipartFile[] file, String filePath, String replyContent, String orderNo) throws IOException {
		YifuUser user = SecurityUtils.getUser();
		if (Common.isEmpty(orderNo)){
			return R.failed(OrderConstants.ORDER_NO_IS_EMPTY);
		}
		if (Common.isEmpty(replyContent) && ArrayUtils.isEmpty(file)){
			return R.failed(OrderConstants.REPLY_CONTENT_AND_ENCLOSURE_IS_EMPTY);
		}
		// 校验回复内容200字以内
		if (!Common.isEmpty(replyContent) && !ValidityUtil.validate200(replyContent)){
			return R.failed(OrderConstants.REPLY_CONTENT_MORE_THAN_200);
		}

		if (ArrayUtils.isNotEmpty(file)){
			if (file.length > CommonConstants.TWENTY_INT){
				return R.failed(OrderConstants.ENCLOSURE_SIZE_ERROR);
			}
			for (MultipartFile multipartFile : file) {
				if(!Common.checkInsuranceFile(multipartFile.getOriginalFilename())){
					return R.failed(OrderConstants.ENCLOSURE_TYPE_IS_ERROR);
				}
				if (multipartFile.getSize() > (CommonConstants.FIFTY_INT*CommonConstants.BYTE*CommonConstants.BYTE)){
					return R.failed(OrderConstants.ENCLOSURE_SIZE_ERROR);
				}
			}

		}

		TOrderReply reply = new TOrderReply();
		reply.setOrderNo(orderNo);
		reply.setReplyContent(Common.isEmpty(replyContent)?null:replyContent);
		reply.setCreateName(user.getNickname());
		reply.setCreateTime(LocalDateTime.now());
		reply.setDeleteFlag(CommonConstants.ZERO_INT);
		boolean save = tOrderReplyService.save(reply);
		//todo 同步推送ekp
		if (save && ArrayUtils.isNotEmpty(file)){
			for (MultipartFile multipartFile : file) {
				String fileName = System.currentTimeMillis() + "_" + multipartFile.getOriginalFilename();
				//filePath不传默认存储空间的根目录
				//支持的附件格式
				String key = "";
				if (!Common.isNotNull(filePath)) {
					key = fileName;
				} else {
					key = filePath + "/" + fileName;
				}
				boolean flag = ossUtil.uploadFileByStream(multipartFile.getInputStream(), key, null);
				if (flag) {
					log.info("文件：" + fileName + "上传至存储空间" + ossUtil.getBucketName() + "成功！");
					TOrderEnclosure orderEnclosure = new TOrderEnclosure();
					orderEnclosure.setOrderNo(reply.getId());
					orderEnclosure.setEnclosureName(multipartFile.getOriginalFilename());
					orderEnclosure.setEnclosureFlag(CommonConstants.ONE_INT);
					orderEnclosure.setEnclosureAddress(key);
					orderEnclosure.setDeleteFlag(CommonConstants.ZERO_INT);
					try {
						tOrderEnclosureService.save(orderEnclosure);
					} catch (Exception e) {
						log.error("OSS文件上传接口异常：" + e.getMessage());
						ossUtil.deleteObject(null, key);
						return R.failed("failed:" + e.getMessage());
					}
				} else {
					return R.failed(OrderConstants.OPERATE_FAILED);
				}
			}
		}
		return R.ok(OrderConstants.OPERATE_SUCCESS);
	}

	/**
	 * 订单回复列表分页查询
	 *
	 * @param page
	 * @param orderNo
	 * @return {@link IPage<OrderReplyListVO>}
	 * @author licancan
	 */
	@Override
	public IPage<OrderReplyListVO> getOrderReplyPageList(Page<OrderReplyListVO> page, String orderNo) {
		IPage<OrderReplyListVO> orderReplyList = baseMapper.getOrderReplyPageList(page,orderNo);
		if (CollectionUtils.isNotEmpty(orderReplyList.getRecords())){
			orderReplyList.getRecords().stream().forEach(e ->{
				List<TOrderEnclosure> list = tOrderEnclosureService.list(Wrappers.<TOrderEnclosure>query().lambda()
						.eq(TOrderEnclosure::getOrderNo, e.getId())
						.eq(TOrderEnclosure::getEnclosureFlag,CommonConstants.ONE_INT)
						.eq(TOrderEnclosure::getDeleteFlag,CommonConstants.ZERO_INT)
				);
				if (CollectionUtils.isNotEmpty(list)){
					e.setOrderReplyEnclosure(list);
				}else {
					e.setOrderReplyEnclosure(Lists.newArrayList());
				}
			});
		}
		return orderReplyList;
	}

}
