package com.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.utils.ValidatorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
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.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;

import com.entity.CheliangdanganEntity;
import com.entity.view.CheliangdanganView;

import com.service.CheliangdanganService;
import com.service.TokenService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MPUtil;
import com.utils.MapUtils;
import com.utils.CommonUtil;
import java.io.IOException;

/**
 * 车辆档案
 * 后端接口
 * @author 
 * @email 
 * @date 2030-03-20 23:07:14
 */
@RestController
@RequestMapping("/cheliangdangan")
public class CheliangdanganController {
    @Autowired
    private CheliangdanganService cheliangdanganService;




    



    /**
     * 后端列表
     */
    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params,CheliangdanganEntity cheliangdangan,
		HttpServletRequest request){
		String tableName = request.getSession().getAttribute("tableName").toString();
		if(tableName.equals("jiashiyuan")) {
			cheliangdangan.setZhanghao((String)request.getSession().getAttribute("username"));
		}
		if(tableName.equals("gongzuorenyuan")) {
			cheliangdangan.setYuangongzhanghao((String)request.getSession().getAttribute("username"));
		}
        EntityWrapper<CheliangdanganEntity> ew = new EntityWrapper<CheliangdanganEntity>();

		PageUtils page = cheliangdanganService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, cheliangdangan), params), params));

        return R.ok().put("data", page);
    }
    
    /**
     * 前端列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,CheliangdanganEntity cheliangdangan, 
		HttpServletRequest request){
        EntityWrapper<CheliangdanganEntity> ew = new EntityWrapper<CheliangdanganEntity>();

		PageUtils page = cheliangdanganService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, cheliangdangan), params), params));
        return R.ok().put("data", page);
    }



	/**
     * 列表
     */
    @RequestMapping("/lists")
    public R list( CheliangdanganEntity cheliangdangan){
       	EntityWrapper<CheliangdanganEntity> ew = new EntityWrapper<CheliangdanganEntity>();
      	ew.allEq(MPUtil.allEQMapPre( cheliangdangan, "cheliangdangan")); 
        return R.ok().put("data", cheliangdanganService.selectListView(ew));
    }

	 /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(CheliangdanganEntity cheliangdangan){
        EntityWrapper< CheliangdanganEntity> ew = new EntityWrapper< CheliangdanganEntity>();
 		ew.allEq(MPUtil.allEQMapPre( cheliangdangan, "cheliangdangan")); 
		CheliangdanganView cheliangdanganView =  cheliangdanganService.selectView(ew);
		return R.ok("查询车辆档案成功").put("data", cheliangdanganView);
    }
	
    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        CheliangdanganEntity cheliangdangan = cheliangdanganService.selectById(id);
        return R.ok().put("data", cheliangdangan);
    }

    /**
     * 前端详情
     */
    @RequestMapping("/detail/{id}")
    public R detail(@PathVariable("id") Long id){
        CheliangdanganEntity cheliangdangan = cheliangdanganService.selectById(id);
        return R.ok().put("data", cheliangdangan);
    }
    



    /**
     * 后端保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CheliangdanganEntity cheliangdangan, HttpServletRequest request){
        if(cheliangdanganService.selectCount(new EntityWrapper<CheliangdanganEntity>().eq("chepaihao", cheliangdangan.getChepaihao()))>0) {
            return R.error("车牌号已存在");
        }
        if(cheliangdanganService.selectCount(new EntityWrapper<CheliangdanganEntity>().eq("fadongjihao", cheliangdangan.getFadongjihao()))>0) {
            return R.error("发动机号已存在");
        }
        if(cheliangdanganService.selectCount(new EntityWrapper<CheliangdanganEntity>().eq("chejiahao", cheliangdangan.getChejiahao()))>0) {
            return R.error("车架号已存在");
        }
    	//ValidatorUtils.validateEntity(cheliangdangan);
        cheliangdanganService.insert(cheliangdangan);
        return R.ok();
    }
    
    /**
     * 前端保存
     */
    @RequestMapping("/add")
    public R add(@RequestBody CheliangdanganEntity cheliangdangan, HttpServletRequest request){
        if(cheliangdanganService.selectCount(new EntityWrapper<CheliangdanganEntity>().eq("chepaihao", cheliangdangan.getChepaihao()))>0) {
            return R.error("车牌号已存在");
        }
        if(cheliangdanganService.selectCount(new EntityWrapper<CheliangdanganEntity>().eq("fadongjihao", cheliangdangan.getFadongjihao()))>0) {
            return R.error("发动机号已存在");
        }
        if(cheliangdanganService.selectCount(new EntityWrapper<CheliangdanganEntity>().eq("chejiahao", cheliangdangan.getChejiahao()))>0) {
            return R.error("车架号已存在");
        }
    	//ValidatorUtils.validateEntity(cheliangdangan);
        cheliangdanganService.insert(cheliangdangan);
        return R.ok();
    }





    /**
     * 修改
     */
    @RequestMapping("/update")
    @Transactional
    public R update(@RequestBody CheliangdanganEntity cheliangdangan, HttpServletRequest request){
        //ValidatorUtils.validateEntity(cheliangdangan);
        if(cheliangdanganService.selectCount(new EntityWrapper<CheliangdanganEntity>().ne("id", cheliangdangan.getId()).eq("chepaihao", cheliangdangan.getChepaihao()))>0) {
            return R.error("车牌号已存在");
        }
        if(cheliangdanganService.selectCount(new EntityWrapper<CheliangdanganEntity>().ne("id", cheliangdangan.getId()).eq("fadongjihao", cheliangdangan.getFadongjihao()))>0) {
            return R.error("发动机号已存在");
        }
        if(cheliangdanganService.selectCount(new EntityWrapper<CheliangdanganEntity>().ne("id", cheliangdangan.getId()).eq("chejiahao", cheliangdangan.getChejiahao()))>0) {
            return R.error("车架号已存在");
        }
        cheliangdanganService.updateById(cheliangdangan);//全部更新
        return R.ok();
    }



    

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        cheliangdanganService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
    
	










}
