InspectController.java 3.82 KB
package vion.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vion.Global;
import vion.enums.ResultEnum;
import vion.model.Inspect;
import vion.service.IInspectSerrvice;
import vion.utils.ResultUtil;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(Global.BASE_URL)
@Slf4j
public class InspectController {

    @Autowired
    private IInspectSerrvice inspectSerrvice;

    @GetMapping("/inspects")
    @ResponseBody
    public Object getProductList(@RequestParam(name = "store_id")Integer store_id,
                                 @RequestParam(name = "status",required=false)Integer status,
                                 @RequestParam(name = "startdate",required=false) Date startdate,
                                 @RequestParam(name = "enddate",required=false)Date enddate,
                                 @RequestParam(name = "pageNum")Integer pageNum,
                                 @RequestParam(name = "pageSize")Integer pageSize){
        //pageNum当前页,pageSize每页条数
        PageHelper.startPage(pageNum,pageSize);

        QueryWrapper<Inspect> wrapper=new QueryWrapper<>();
        wrapper.eq("store_id",store_id);
        if(status!=null)
            wrapper.eq("status",status);
        if(startdate!=null)
            wrapper.ge("inspect_date",startdate);
        if(enddate!=null)
            wrapper.le("inspect_date",enddate);

        List<Inspect> inspectList=inspectSerrvice.list(wrapper);
        PageInfo pageInfo=new PageInfo(inspectList,10);

        if (inspectList != null) {
            return ResultUtil.success(pageInfo);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @GetMapping("/inspect")
    @ResponseBody
    public Object getProductByID(@RequestParam(name = "id")Integer id){

        Inspect inspect=inspectSerrvice.getById(id);

        if (inspect != null) {
            return ResultUtil.success(inspect);
        } else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @PostMapping("/inspects")
    @ResponseBody
    public Object saveOrUpdate(@RequestBody Inspect data) {

        try {
            if(data.getId()==null){
                inspectSerrvice.save(data);
                return ResultUtil.success();
            }else{
                data.setModifyTime(new Date());
                Inspect inspect=inspectSerrvice.saveAndReturn(data);
                return ResultUtil.success(inspect);
            }

        }catch (Exception e) {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }

    @PostMapping("/updateInspectStatus")
    @ResponseBody
    public Object UpdateStatus(@RequestBody Inspect data) {

        Date current=new Date();

        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.eq("id",data.getId());
        updateWrapper.set("status",data.getStatus());
        updateWrapper.set("modify_time",current);
        updateWrapper.set("audit_date",current);
        updateWrapper.set("reviewer",data.getReviewer());
        //审核状态为已完成的时候更新完成时间(0进行中、1待审核、2已完成、3、驳回)
        if(data.getStatus()==2){
            updateWrapper.set("finish_date",current);
        }else{
            updateWrapper.set("finish_date",null);
        }
        boolean result= inspectSerrvice.update(updateWrapper);
        if(result){
            return ResultUtil.success();
        }else {
            return ResultUtil.error(ResultEnum.SELECT_ERROR);
        }
    }
}