package com.gci.boot.modules.projects.controller;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import cn.hutool.core.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.annotation.Query.handle.QueryGenerator;
import com.gci.boot.common.log.annotation.AddSysLog;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.projects.entity.TbVehicleCheckDetail;
import com.gci.boot.modules.projects.service.ITbVehicleCheckDetailService;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 车辆验收车辆详情 前端控制器
 * </p>
 *
 * @author xushuhan
 * @since 2023-09-11
 */
@Slf4j
@RestController
@RequestMapping("/projects/vehicleCheckDetail")
public class TbVehicleCheckDetailController {

	@Autowired
	private ITbVehicleCheckDetailService tbVehicleCheckProjectService;

	@AddSysLog(value = "车辆验收车辆详情-列表查询")
	@GetMapping("/list")
	public Result<?> list(TbVehicleCheckDetail item) {
		LambdaQueryWrapper<TbVehicleCheckDetail> queryWrapper = QueryGenerator.initQueryWrapper(TbVehicleCheckDetail.class, item);
		/*LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if(sysUser!=null) {
			queryWrapper.likeRight(StringUtils.hasLength(sysUser.getOrgCode()),TbVehicleCheckDetail::getSysOrgCode, sysUser.getOrgCode());
		}*/
		//queryWrapper.eq(TbVehicleCheckDetail::getCancelFlag, CommonConstant.STATUS_1);
		queryWrapper.orderByDesc(TbVehicleCheckDetail::getCreateTime);
		List<TbVehicleCheckDetail> list = tbVehicleCheckProjectService.list(queryWrapper);
		return Result.ok(list);
	}

	@AddSysLog(value = "车辆验收车辆详情-分页列表查询")
	@GetMapping("/page")
	public Result<?> page(TbVehicleCheckDetail item,@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
						  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

		LambdaQueryWrapper<TbVehicleCheckDetail> queryWrapper = QueryGenerator.initQueryWrapper(TbVehicleCheckDetail.class, item);
		/*LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		if(sysUser!=null) {
			queryWrapper.likeRight(StringUtils.hasLength(sysUser.getOrgCode()),TbVehicleCheckDetail::getSysOrgCode, sysUser.getOrgCode());
		}*/
		queryWrapper.orderByDesc(TbVehicleCheckDetail::getCreateTime);
		Page<TbVehicleCheckDetail> page = new Page<TbVehicleCheckDetail>(pageNo, pageSize);
		IPage<TbVehicleCheckDetail> pageList = tbVehicleCheckProjectService.page(page,queryWrapper);
		return Result.ok(pageList);
	}

	@AddSysLog(value = "车辆验收车辆详情-添加")
	@PostMapping("/add")
	public Result<?> add(@RequestBody TbVehicleCheckDetail item){
		tbVehicleCheckProjectService.save(item);
		return Result.ok();
	}

	@AddSysLog(value = "车辆验收车辆详情-编辑")
	@PutMapping("/edit")
	public Result<?> edit(@RequestBody TbVehicleCheckDetail item){
		tbVehicleCheckProjectService.updateById(item);
		return Result.ok();
	}

	@AddSysLog(value = "车辆验收车辆详情-删除")
	@DeleteMapping("/delete")
	public Result<?> delete(TbVehicleCheckDetail item){
		tbVehicleCheckProjectService.removeById(item.getId());
		return Result.ok();
	}
	
	/**
	 * 多次读入
	 * @param request
	 * @return
	 */
	@AddSysLog(value = "车辆验收车辆详情-导入")
	@PostMapping("/simple/import")
	public Result<?> simpleImport(HttpServletRequest request) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			try(InputStream input = file.getInputStream()) {
				List<TbVehicleCheckDetail> list = EasyExcel.read(input).head(TbVehicleCheckDetail.class).sheet().doReadSync();
				for (TbVehicleCheckDetail tbVehicleCheckDetail : list) {
					if (NumberUtil.isNumber(tbVehicleCheckDetail.getCurbWeightString())) {
						tbVehicleCheckDetail.setCurbWeight(NumberUtil.toBigDecimal(tbVehicleCheckDetail.getCurbWeightString()));
					} else {
						tbVehicleCheckDetail.setCurbWeight(null);
					}
				}
				return Result.ok(list);
			}catch (IOException e){
				log.error(e.getMessage(), e);
				return Result.error("导入错误！");
			}
		}
		return Result.ok();
	}

}
