package com.foreverwin.mesnac.equip.controller;

import com.foreverwin.mesnac.common.service.InspectionItemDetailService;
import com.foreverwin.mesnac.common.service.InspectionItemService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.FrontPage;
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.equip.service.ResourceInspectPlanService;
import com.foreverwin.mesnac.equip.model.ResourceInspectPlan;

import java.util.List;

/**
 *
 * @author pavel.Liu
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/Z-RESOURCE-INSPECT-PLAN")
public class ResourceInspectPlanController {

    @Autowired
    public ResourceInspectPlanService resourceInspectPlanService;

    @Autowired
    public InspectionItemService inspectionItemService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<ResourceInspectPlan> frontPage, ResourceInspectPlan resourceInspectPlan){
        IPage result;
        QueryWrapper<ResourceInspectPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceInspectPlan);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(ResourceInspectPlan::getHandle, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getSite, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getPlanNo, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getRevision, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getCurrentRevision, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getDescription, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getStatus, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getCategory, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getScheduleType, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getFrequencyUnit, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getInspectionItemBo, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getCreateUser, frontPage.getGlobalQuery())
                .or().like(ResourceInspectPlan::getModifyUser, frontPage.getGlobalQuery())
    );
        }
        result = resourceInspectPlanService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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


    /**
     * 新增或更新：依据handle是否存在，存在即更新数据，否则新增
     * @param resourceInspectPlan  传递的实体
     * @return
     */
    @PostMapping(value = "/saveOrUpdate")
    public R saveOrUpdate(@RequestBody ResourceInspectPlan resourceInspectPlan) {
        try{
            return R.ok(resourceInspectPlanService.saveResourcePlanData(resourceInspectPlan));
        }catch (Exception e){
            return R.failed("更新或保存失败：" + e.getMessage());
        }
    }

    /**
     * 根据handle，查询检验计划
     * @param resourceInspectPlan
     * @return
     */
    @ResponseBody
    @RequestMapping("/getInspectPlanByHandle")
    public R getInspectPlanByHandle(ResourceInspectPlan resourceInspectPlan){
        return R.ok(resourceInspectPlanService.getInspectPlanByHandle(resourceInspectPlan));
    }

    /**
     * 根据handle，删除检验项目计划
     * @param resourceInspectPlan
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteInspectionPlanByHandle")
    public R deleteInspectionPlanByHandle(ResourceInspectPlan resourceInspectPlan){
        try {
            resourceInspectPlanService.deleteInspectionPlanByHandle(resourceInspectPlan);
        } catch (Exception e) {
            return R.failed("计划删除失败：" + e.getMessage());
        }
        return R.ok();
    }

}