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.BaoxianxinxiEntity;
import com.entity.view.BaoxianxinxiView;

import com.service.BaoxianxinxiService;
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("/baoxianxinxi")
public class BaoxianxinxiController {
    @Autowired
    private BaoxianxinxiService baoxianxinxiService;




    



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

		PageUtils page = baoxianxinxiService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, baoxianxinxi), params), params));

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

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



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

	 /**
     * 查询
     */
    @RequestMapping("/query")
    public R query(BaoxianxinxiEntity baoxianxinxi){
        EntityWrapper< BaoxianxinxiEntity> ew = new EntityWrapper< BaoxianxinxiEntity>();
 		ew.allEq(MPUtil.allEQMapPre( baoxianxinxi, "baoxianxinxi")); 
		BaoxianxinxiView baoxianxinxiView =  baoxianxinxiService.selectView(ew);
		return R.ok("查询保险信息成功").put("data", baoxianxinxiView);
    }
	
    /**
     * 后端详情
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        BaoxianxinxiEntity baoxianxinxi = baoxianxinxiService.selectById(id);
        return R.ok().put("data", baoxianxinxi);
    }

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



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





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



    

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










}
