package com.foreverwin.mesnac.anomaly.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.anomaly.model.UploadPictures;
import com.foreverwin.mesnac.anomaly.service.UploadPicturesService;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author zjw
 * @since 2021-09-09
 */
@RestController
@RequestMapping("/Z-UPLOAD-PICTURES")
public class UploadPicturesController {

    @Autowired
    public UploadPicturesService uploadPicturesService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getUploadPicturesById(@PathVariable String id) {
        return R.ok( uploadPicturesService.getById(id));
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getUploadPicturesList(UploadPictures uploadPictures){
        List<UploadPictures> result;
        QueryWrapper<UploadPictures> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(uploadPictures);
        result = uploadPicturesService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<UploadPictures> frontPage, UploadPictures uploadPictures){
        IPage result;
        QueryWrapper<UploadPictures> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(uploadPictures);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(UploadPictures::getHandle, frontPage.getGlobalQuery())
                .or().like(UploadPictures::getSite, frontPage.getGlobalQuery())
                .or().like(UploadPictures::getPicturePath, frontPage.getGlobalQuery())
                .or().like(UploadPictures::getPictureAddress, frontPage.getGlobalQuery())
                .or().like(UploadPictures::getObjectBo, frontPage.getGlobalQuery())
                .or().like(UploadPictures::getNum, frontPage.getGlobalQuery())
                .or().like(UploadPictures::getType, frontPage.getGlobalQuery())
                .or().like(UploadPictures::getCreatedUser, frontPage.getGlobalQuery())
                .or().like(UploadPictures::getModifiedUser, frontPage.getGlobalQuery())
                .or().like(UploadPictures::getStatus, frontPage.getGlobalQuery())
    );
        }
        result = uploadPicturesService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

    /**
     * 新增
     * @param uploadPictures  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping
    public R save(@RequestBody UploadPictures uploadPictures) {
        return R.ok(uploadPicturesService.save(uploadPictures));
    }

    /**
     * 修改
     * @param uploadPictures  传递的实体
     * @return  null 失败  实体成功
     */
    @PutMapping
    public R updateById(@RequestBody UploadPictures uploadPictures) {
        return R.ok(uploadPicturesService.updateById(uploadPictures));
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(uploadPicturesService.removeById(id));
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/delete-batch")
    public R removeByIds(List<String> ids){
        return R.ok(uploadPicturesService.removeByIds(ids));
    }
}