package com.foreverwin.mesnac.meapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.meapi.model.Resrce;
import com.foreverwin.mesnac.meapi.service.ResrceService;
import com.foreverwin.modular.core.util.CommonMethods;
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 Leon.L
 * @since 2021-05-26
 */
@RestController
    @RequestMapping("/RESOURCE")
public class ResrceController {

    @Autowired
    public ResrceService resrceService;


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

    @ResponseBody
    @GetMapping("getEnabledResourceList")
    public R getEnabledResourceList(Resrce resrce) {
        List<Resrce> result;
        try {
            String site = CommonMethods.getSite();
            //已启用状态
            String status301Bo = "StatusBO:" + site + ",301";

            resrce.setSite(site);
            resrce.setStatusBo(status301Bo);
            QueryWrapper<Resrce> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(resrce);
            result = resrceService.list(queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

    @GetMapping("/getResourceListByType")
    public R getResourceListByType(String resourceType) {
        List<Resrce> result;
        try {
            String site = CommonMethods.getSite();
            result = resrceService.getResourceListByType(site, resourceType);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<Resrce> frontPage, Resrce resrce){
        IPage result;
        try {
            String site = CommonMethods.getSite();
            resrce.setSite(site);

            QueryWrapper<Resrce> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(resrce);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                queryWrapper
                        .like(Resrce.RESRCE, frontPage.getGlobalQuery())
                        .or().like(Resrce.DESCRIPTION, frontPage.getGlobalQuery())
                        .or().like(Resrce.STATUS_BO, frontPage.getGlobalQuery())
                        .or().like(Resrce.PROCESS_RESOURCE, frontPage.getGlobalQuery())
                        .or().like(Resrce.OPERATION_BO, frontPage.getGlobalQuery())
                        .or().like(Resrce.SETUP_STATE, frontPage.getGlobalQuery())
                        .or().like(Resrce.SETUP_DESCRIPTION, frontPage.getGlobalQuery())
                        .or().like(Resrce.CNC_MACHINE, frontPage.getGlobalQuery());
            }
            result = resrceService.page(frontPage.getPagePlus(), queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
        return R.ok(result);
    }

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

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

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


    /**
     * 不分页查询
     * @return
     */
    @GetMapping("findAllResrce")
    public R findAllResrce(){
        return R.ok(resrceService.findAllResrce());
    }

    /**
     * 根据设备，找车间信息
     * @return
     */
    @GetMapping("findResourceAndWorkCenterByResource")
    public R findResourceAndWorkCenterByResource(Resrce resrce){
        return R.ok(resrceService.findResourceAndWorkCenterByResource(resrce));
    }


    @GetMapping("/getResourceListByUT")
    public R getResourceListByUT(String user, String resourceType) {
        List<Resrce> result;
        try {
            String site = CommonMethods.getSite();
            String []userArray = user.split(",");
            result = resrceService.getResourceListByUT(site, resourceType, userArray);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }


    /**
     * 功能1  1. 找到设备下面的某个字段，弹出框的不分页查询，并且自定义字段的值不能为空
     * 功能2  2. 根据设备找到它的某个字段的值
     * @param resource 资源名
     * @param customField 字段名
     * @return
     */
    @GetMapping("/findResourceCustomList")
    public R findResourceCustomList(String resource,String customField){
        try{
            return R.ok(resrceService.findResourceCustomList(resource,customField));
        }catch (Exception e){
            return R.failed(e.getMessage());
        }
    }
}