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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.springblade.common.cache.DictBizCache;
import org.springblade.constant.BusinessConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.ConcurrentDateFormat;
import org.springblade.core.tool.utils.Func;
import org.springblade.enums.OrderStatusEnum;
import org.springblade.modules.business.check.order.dto.CheckOrderInfoDTO;
import org.springblade.modules.business.check.order.entity.CheckOrderInfoEntity;
import org.springblade.modules.business.check.order.service.IRtuCheckOrderService;
import org.springblade.modules.business.check.order.vo.CheckOrderInfoVO;
import org.springblade.modules.business.check.order.wrapper.RtuCheckOrderWrapper;
import org.springblade.modules.business.check.process.dto.OrderProcessInfoDTO;
import org.springblade.modules.business.check.process.entity.OrderProcessInfoEntity;
import org.springblade.modules.business.check.process.service.IRtuOrderProcessService;
import org.springblade.modules.business.check.process.vo.OrderProcessInfoVO;
import org.springblade.modules.business.check.process.wrapper.RtuOrderProcessWrapper;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/***
 * Date:2022/8/22
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/rtu/check/order/process")
@AllArgsConstructor
@Api(value = "工单处理管理模块", tags = "工单处理管理模块")
public class RtuOrderProcessController extends BladeController {
	private final IRtuOrderProcessService rtuOrderProcessService;
	private final IRtuCheckOrderService checkOrderService;
	private final IUserService userService;

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "orderId", value = "工单ID", paramType = "query", dataType = "long")
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "不分页", notes = "传入checkOrderInfoDTO")
	public R<List<OrderProcessInfoVO>> list(OrderProcessInfoDTO orderProcessInfoDTO) {
		LambdaQueryWrapper<OrderProcessInfoEntity> wrapper = Wrappers.<OrderProcessInfoEntity>query().lambda();
		wrapper.eq(OrderProcessInfoEntity::getOrderId,orderProcessInfoDTO.getOrderId());
		wrapper.orderByAsc(OrderProcessInfoEntity::getCreateTime);
		List<OrderProcessInfoEntity> list = rtuOrderProcessService.list(wrapper);
		return R.data(RtuOrderProcessWrapper.build().listVO(list));
	}

	/**
	 * 查询当天填报的工单信息
	 *
	 * @return
	 */
	@GetMapping("/today/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<List<OrderProcessInfoVO>> todayList() {
		BladeUser user = AuthUtil.getUser();
		OrderProcessInfoDTO processInfoDTO = new OrderProcessInfoDTO();
		if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_SERVICE_PERSON.equals(user.getRoleName())) {
			processInfoDTO.setOrgId(Func.toLong(user.getDeptId()));
		} else {
			processInfoDTO.setOrgId(null);
		}
		Date date = new Date();
		String dt = Func.formatDateTime(date);
		processInfoDTO.setCreateTime(Func.parse(dt, ConcurrentDateFormat.of("yyyy-MM-dd")));
		List<OrderProcessInfoVO> list = rtuOrderProcessService.selectTodayProcessList(processInfoDTO);
		return R.data(list);
	}


	/**
	 * 多表联合查询自定义分页
	 */
	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "工单ID", paramType = "query", dataType = "long")
	})
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入checkOrderInfoDTO")
	public R<IPage<OrderProcessInfoVO>> page(CheckOrderInfoDTO checkOrderInfoDTO, Query query) {
		IPage<OrderProcessInfoVO> pages = rtuOrderProcessService.selectPage(Condition.getPage(query), checkOrderInfoDTO);
		List<OrderProcessInfoVO> list = pages.getRecords();
		for (OrderProcessInfoVO v : list) {
			RtuOrderProcessWrapper.build().addInfoVO(v);
		}
		return R.data(pages);
	}


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "填报ID", paramType = "query", dataType = "long"),
	})
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入orderProcessInfoEntity")
	public R<OrderProcessInfoVO> detail(OrderProcessInfoEntity orderProcessInfoEntity) {
		OrderProcessInfoEntity detail = rtuOrderProcessService.getOne(Condition.getQueryWrapper(orderProcessInfoEntity));
		OrderProcessInfoVO vo = RtuOrderProcessWrapper.build().entityVO(detail);
		CheckOrderInfoEntity checkOrderInfoEntity = checkOrderService.getById(vo.getOrderId());
		if (checkOrderInfoEntity.getOrderConfirmUser() != null){
			User user = userService.getById(checkOrderInfoEntity.getOrderConfirmUser());
			vo.setOrderConfirmUserName(user.getRealName());
		}
		DictBiz biz = DictBizCache.getById(checkOrderInfoEntity.getOrderFromKey());
		vo.setOrderFromText(biz.getDictValue());
		return R.data(vo);
	}


	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入orderProcessInfoEntity")
	public R save(@RequestBody OrderProcessInfoEntity orderProcessInfoEntity) {
		CheckOrderInfoEntity checkOrderInfoEntity = new CheckOrderInfoEntity();
		checkOrderInfoEntity.setId(orderProcessInfoEntity.getOrderId());
		List<DictBiz> statusdicts = DictBizCache.getList("rtu_check_order_status");
		for (DictBiz biz : statusdicts) {
			if (biz.getDictKey().equals("" + OrderStatusEnum.STATUS_PROCESS.getCode())) {
				checkOrderInfoEntity.setOrderStatusKey(biz.getId());
				break;
			}
		}

		checkOrderInfoEntity.setOrderDesc(orderProcessInfoEntity.getProcessDesc());
		checkOrderService.updateOrderStatus(checkOrderInfoEntity);

		orderProcessInfoEntity.setOrderStatusKey(checkOrderInfoEntity.getOrderStatusKey());
		return R.status(rtuOrderProcessService.save(orderProcessInfoEntity));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入orderProcessInfoEntity")
	public R update(@RequestBody OrderProcessInfoEntity orderProcessInfoEntity) {
		return R.status(rtuOrderProcessService.updateById(orderProcessInfoEntity));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入orderProcessInfoEntity")
	public R submit(@RequestBody OrderProcessInfoEntity orderProcessInfoEntity) {
		return R.status(rtuOrderProcessService.saveOrUpdate(orderProcessInfoEntity));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入projectInfoEntity")
	public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
		boolean temp = rtuOrderProcessService.deleteLogic(Func.toLongList(ids));
		return R.status(temp);
	}


}
