package com.foreverwin.mesnac.production.controller;

import com.foreverwin.mesnac.meapi.util.StringUtils;
import com.foreverwin.modular.core.exception.BusinessException;
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.production.service.SfcDataService;
import com.foreverwin.mesnac.production.model.SfcData;
import java.util.List;

/**
 *
 * @author Leon.L
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/SFC-DATA")
public class SfcDataController {

    @Autowired
    public SfcDataService sfcDataService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<SfcData> frontPage, SfcData sfcData){
        IPage result;
        QueryWrapper<SfcData> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sfcData);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(SfcData::getHandle, frontPage.getGlobalQuery())
                .or().like(SfcData::getSfcBo, frontPage.getGlobalQuery())
                .or().like(SfcData::getDataField, frontPage.getGlobalQuery())
                .or().like(SfcData::getDataAttr, frontPage.getGlobalQuery())
                .or().like(SfcData::getUserBo, frontPage.getGlobalQuery())
    );
        }
        result = sfcDataService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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


    @ResponseBody
    @GetMapping("/saveSfcLocation")
    public R saveSfcLocation(String sfc, String location) {
        try {
            if (StringUtils.isBlank(sfc)) {
                throw BusinessException.build("SFC不能为空");
            }
            if (StringUtils.isBlank(location)) {
                throw BusinessException.build("位置不能为空");
            }

            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            sfcDataService.saveSfcLocation(site, user, sfc, location);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok();
    }
}