package com.foreverwin.mesnac.meapi.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.meapi.service.DcParameterService;
import com.foreverwin.mesnac.meapi.model.DcParameter;
import java.util.List;

/**
 *
 * @author pavel.liu
 * @since 2021-07-13
 */
@RestController
@RequestMapping("/DC-PARAMETER")
public class DcParameterController {

    @Autowired
    public DcParameterService dcParameterService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<DcParameter> frontPage, DcParameter dcParameter){
        IPage result;
        QueryWrapper<DcParameter> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(dcParameter);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(DcParameter::getHandle, frontPage.getGlobalQuery())
                .or().like(DcParameter::getDcGroupBo, frontPage.getGlobalQuery())
                .or().like(DcParameter::getParameterName, frontPage.getGlobalQuery())
                .or().like(DcParameter::getDataType, frontPage.getGlobalQuery())
                .or().like(DcParameter::getStatus, frontPage.getGlobalQuery())
                .or().like(DcParameter::getAllowMissingValue, frontPage.getGlobalQuery())
                .or().like(DcParameter::getPerformSpc, frontPage.getGlobalQuery())
                .or().like(DcParameter::getDisplayChart, frontPage.getGlobalQuery())
                .or().like(DcParameter::getDescription, frontPage.getGlobalQuery())
                .or().like(DcParameter::getUnits, frontPage.getGlobalQuery())
                .or().like(DcParameter::getSpcChartBo, frontPage.getGlobalQuery())
                .or().like(DcParameter::getDisplayDataInformation, frontPage.getGlobalQuery())
                .or().like(DcParameter::getOverrideMinMax, frontPage.getGlobalQuery())
                .or().like(DcParameter::getDcValueMask, frontPage.getGlobalQuery())
                .or().like(DcParameter::getExpressionBuilder, frontPage.getGlobalQuery())
                .or().like(DcParameter::getShortRun, frontPage.getGlobalQuery())
                .or().like(DcParameter::getOpcAcquireAt, frontPage.getGlobalQuery())
                .or().like(DcParameter::getOpcServer, frontPage.getGlobalQuery())
                .or().like(DcParameter::getOpcDevice, frontPage.getGlobalQuery())
                .or().like(DcParameter::getOpcTag, frontPage.getGlobalQuery())
                .or().like(DcParameter::getOpcServerHostname, frontPage.getGlobalQuery())
                .or().like(DcParameter::getOpcServerModel, frontPage.getGlobalQuery())
                .or().like(DcParameter::getOpcServerProgid, frontPage.getGlobalQuery())
                .or().like(DcParameter::getOpcServerClsid, frontPage.getGlobalQuery())
                .or().like(DcParameter::getOpcDeviceId, frontPage.getGlobalQuery())
                .or().like(DcParameter::getOpcTagId, frontPage.getGlobalQuery())
                .or().like(DcParameter::getScriptBo, frontPage.getGlobalQuery())
                .or().like(DcParameter::getBooleanZeroValue, frontPage.getGlobalQuery())
                .or().like(DcParameter::getBooleanOneValue, frontPage.getGlobalQuery())
                .or().like(DcParameter::getErpQmCharType, frontPage.getGlobalQuery())
                .or().like(DcParameter::getErpIsQmCritical, frontPage.getGlobalQuery())
                .or().like(DcParameter::getSoftLimitCheck, frontPage.getGlobalQuery())
                .or().like(DcParameter::getAutoLogNc, frontPage.getGlobalQuery())
                .or().like(DcParameter::getNcCodeBo, frontPage.getGlobalQuery())
                .or().like(DcParameter::getDataFieldBo, frontPage.getGlobalQuery())
    );
        }
        result = dcParameterService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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