package com.xxfc.platform.vehicle.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreClientToken;
import com.xxfc.platform.vehicle.biz.AccompanyingItemBiz;
import com.xxfc.platform.vehicle.common.BaseController;
import com.xxfc.platform.vehicle.common.CustomIllegalParamException;
import com.xxfc.platform.vehicle.common.RestResponse;
import com.xxfc.platform.vehicle.constant.ResCode.ResCode;
import com.xxfc.platform.vehicle.entity.AccompanyingItem;
import com.xxfc.platform.vehicle.pojo.AddOrUpdateAccompanyingItem;
import com.github.wxiaoqi.security.common.vo.PageDataVO;
import com.xxfc.platform.vehicle.pojo.QueryAccompanyItemVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/accompanyingItem")
@IgnoreClientToken
@Slf4j
public class AccompanyingItemController extends BaseController<AccompanyingItemBiz> {


    /**
     *
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping(value ="/{id}")
    public RestResponse<AccompanyingItem> get(@PathVariable Integer id) throws Exception{
        return RestResponse.codeAndData(RestResponse.SUC_CODE,baseBiz.get(id));
    }

    @RequestMapping(value ="/all",method = RequestMethod.GET)
    public RestResponse<List<AccompanyingItem>> getAll(){
        return RestResponse.codeAndData(RestResponse.SUC_CODE,baseBiz.getAll());
    }


    @RequestMapping(value ="/page",method = RequestMethod.GET)
    public RestResponse<PageDataVO<AccompanyingItem>> getByPage(@RequestParam String queryAccompanyItemVoJson) throws Exception{
        QueryAccompanyItemVo queryAccompanyItemVo = null;
        try {
            queryAccompanyItemVo = JSON.parseObject(queryAccompanyItemVoJson,QueryAccompanyItemVo.class);
            return RestResponse.data(baseBiz.getByPage(queryAccompanyItemVo));
        } catch (JSONException ex) {
            return RestResponse.code(ResCode.INVALID_REST_REQ_PARAM.getCode());
        } catch (CustomIllegalParamException ex){
            return RestResponse.code(ResCode.INVALID_REST_REQ_PARAM.getCode());
        }
    }

    @RequestMapping(value ="",method = RequestMethod.POST)
    public RestResponse<Integer> add(@RequestBody AddOrUpdateAccompanyingItem addOrUpdateAccompanyingItem) throws Exception{
        return baseBiz.add(addOrUpdateAccompanyingItem);
    }

    @RequestMapping(value ="",method = RequestMethod.PUT)
    public RestResponse<Integer> update(@RequestBody AddOrUpdateAccompanyingItem addOrUpdateAccompanyingItem) throws Exception{
        return baseBiz.update(addOrUpdateAccompanyingItem);
    }

    @RequestMapping(value ="/{id}",method = RequestMethod.DELETE)
    public RestResponse<Integer> del(@PathVariable Integer id) throws Exception{
        return baseBiz.del(id);
    }
}
