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.dto.AbnormalPlanDto;
import com.foreverwin.mesnac.anomaly.model.AbnormalPlan;
import com.foreverwin.mesnac.anomaly.service.AbnormalPlanService;
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-08-07
 */
@RestController
@RequestMapping("/Z-ABNORMAL-PLAN")
public class AbnormalPlanController {

    @Autowired
    public AbnormalPlanService abnormalPlanService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<AbnormalPlan> frontPage, AbnormalPlan abnormalPlan){
        IPage result;
        QueryWrapper<AbnormalPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(abnormalPlan);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(AbnormalPlan::getHandle, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getSite, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getAbnormalNo, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getStatus, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getWorkCenter, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getAbnormalMethod, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getShopOrder, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getResrce, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getItemBo, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getProcessor, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getSendUserGroup, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getClosedUser, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getCreatedUser, frontPage.getGlobalQuery())
                .or().like(AbnormalPlan::getModifiedUser, frontPage.getGlobalQuery())
    );
        }
        result = abnormalPlanService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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

    @GetMapping("/findAbnormalPlan")
    public R findAbnormalPlan(AbnormalPlanDto abnormalPlanDto){
        return R.ok(abnormalPlanService.findAbnormalPlan(abnormalPlanDto));
    }

    @PostMapping("/processes")
    public R processes(@RequestBody List<AbnormalPlan> abnormalPlanList){
        abnormalPlanService.processes(abnormalPlanList);
        return R.ok();
    }
}