package com.foreverwin.mesnac.meapi.controller;

import com.foreverwin.mesnac.meapi.dto.SfcDto;
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 com.foreverwin.mesnac.meapi.model.Sfc;
import com.foreverwin.mesnac.meapi.service.SfcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    @Autowired
    public SfcService sfcService;


    @ResponseBody
    @GetMapping("/findSfcData")
    public R findSfcData(String sfc) {
        SfcDto sfcDto = null;

        try {
            if (StringUtils.isBlank(sfc)) {
                throw BusinessException.build("SFC不能为空");
            }

            String site = CommonMethods.getSite();
            sfcDto = sfcService.findSfcData(site, sfc);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(sfcDto);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getSfcList(Sfc sfc){
        List<Sfc> result;
        sfc.setSite(CommonMethods.getSite());
        QueryWrapper<Sfc> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sfc);
        result = sfcService.list(queryWrapper);
        return R.ok(result);
    }
    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/pageByResrce")
    public R page(FrontPage<Sfc> frontPage, SfcDto sfc){
        IPage result;
        try {
            String site = CommonMethods.getSite();
            sfc.setSite(site);
            QueryWrapper<SfcDto> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(sfc);
            result = sfcService.pageByResrce(frontPage.getPagePlus(), queryWrapper,frontPage.getGlobalQuery());
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }
    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<Sfc> frontPage, Sfc sfc){
        IPage result;
        try {
            String site = CommonMethods.getSite();
            sfc.setSite(site);
            QueryWrapper<Sfc> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(sfc);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                queryWrapper
                        .like(Sfc.HANDLE, frontPage.getGlobalQuery())
                        .or().like(Sfc.SITE, frontPage.getGlobalQuery())
                        .or().like(Sfc.SFC, frontPage.getGlobalQuery())
                        .or().like(Sfc.STATUS_BO, frontPage.getGlobalQuery())
                        .or().like(Sfc.SHOP_ORDER_BO, frontPage.getGlobalQuery())
                        .or().like(Sfc.ITEM_BO, frontPage.getGlobalQuery())
                        .or().like(Sfc.ORIGINAL_STATUS_BO, frontPage.getGlobalQuery())
                        .or().like(Sfc.SN_DONE, frontPage.getGlobalQuery());
            }
            result = sfcService.page(frontPage.getPagePlus(), queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

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

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

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

    @GetMapping("/findOperationBySfc")
    public R findOperationBySfc(String sfc){

        return R.ok(sfcService.findOperationBySfc(sfc));
    }

    @GetMapping("/findSfcByResrce")
    public R findSfcByResrce(String resource,Sfc sfc){
        return R.ok(sfcService.findSfcByResrce(resource,sfc));
    }

}