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


import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
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 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.basedata.entity.TbVehicleCheckTemplateProject;
import com.gci.boot.modules.basedata.service.ITbVehicleCheckTemplateProjectService;
import com.gci.boot.modules.system.vo.LoginUser;

/**
 * <p>
 * 车辆验收模板项目 前端控制器
 * </p>
 *
 * @author xushuhan
 * @since 2023-09-07
 */
@RestController
@RequestMapping("/basedata/vehicleCheckTemplateProject")
public class TbVehicleCheckTemplateProjectController {

	@Autowired
	private ITbVehicleCheckTemplateProjectService tbVehicleCheckTemplateProjectService;


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

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

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

	@AddSysLog(value = "车辆验收模板项目-添加")
	@PostMapping("/add")
	public Result<?> add(@RequestBody TbVehicleCheckTemplateProject item){
		tbVehicleCheckTemplateProjectService.save(item);
		return Result.ok();
	}

	@AddSysLog(value = "车辆验收模板项目-编辑")
	@PutMapping("/edit")
	public Result<?> edit(@RequestBody TbVehicleCheckTemplateProject item){
		tbVehicleCheckTemplateProjectService.updateById(item);
		return Result.ok();
	}

	@AddSysLog(value = "车辆验收模板项目-删除")
	@DeleteMapping("/delete")
	public Result<?> delete(TbVehicleCheckTemplateProject item){
		tbVehicleCheckTemplateProjectService.removeById(item.getId());
		return Result.ok();
	}

	//删除验收项目
	@AddSysLog(value = "车辆验收模板项目-删除验收项目")
	@DeleteMapping("/deleteByName")
	public Result<?> deleteByName(TbVehicleCheckTemplateProject item){
		if(!StringUtils.hasLength(item.getName())) {
			return Result.error("验收项目不能为空");
		}
		if(!StringUtils.hasLength(item.getTemplateId())) {
			return Result.error("模板id不能为空");
		}
		LambdaQueryWrapper<TbVehicleCheckTemplateProject> queryWrapper = QueryGenerator.initQueryWrapper(TbVehicleCheckTemplateProject.class, item);
		queryWrapper.eq(TbVehicleCheckTemplateProject::getTemplateId, item.getTemplateId());
		queryWrapper.eq(TbVehicleCheckTemplateProject::getName, item.getName());
		tbVehicleCheckTemplateProjectService.removeById(item.getId());
		return Result.ok();
	}

}
