package com.foreverwin.mesnac.anomaly.controller;

import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.CommonMethods;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mesnac.anomaly.service.AbnormalDutyService;
import com.foreverwin.mesnac.anomaly.model.AbnormalDuty;
import java.util.List;

/**
 *
 * @author robert
 * @since 2021-07-01
 */
@RestController
@RequestMapping("/Z-ABNORMAL-DUTY")
public class AbnormalDutyController {

    @Autowired
    public AbnormalDutyService abnormalDutyService;

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

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getAbnormalDutyList(AbnormalDuty abnormalDuty){
        List<AbnormalDuty> result;
        String site = CommonMethods.getSite();
        abnormalDuty.setSite(site);
        QueryWrapper<AbnormalDuty> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(abnormalDuty);
        result = abnormalDutyService.list(queryWrapper);
        return R.ok(result);
    }

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

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

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

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(abnormalDutyService.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(abnormalDutyService.removeByIds(ids));
    }
}