package com.foreverwin.mesnac.production.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.common.service.AnomalyService;
import com.foreverwin.mesnac.production.model.SfcScrap;
import com.foreverwin.mesnac.production.service.SfcScrapService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author Leon.L
 * @since 2021-09-07
 */
@RestController
@RequestMapping("/Z-SFC-SCRAP")
public class SfcScrapController {

    @Autowired
    public SfcScrapService sfcScrapService;

    @Autowired
    private AnomalyService anomalyService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<SfcScrap> frontPage, SfcScrap sfcScrap){
        IPage result;
        QueryWrapper<SfcScrap> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sfcScrap);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(SfcScrap::getHandle, frontPage.getGlobalQuery())
                .or().like(SfcScrap::getSite, frontPage.getGlobalQuery())
                .or().like(SfcScrap::getSfc, frontPage.getGlobalQuery())
                .or().like(SfcScrap::getOperation, frontPage.getGlobalQuery())
                .or().like(SfcScrap::getStepId, frontPage.getGlobalQuery())
                .or().like(SfcScrap::getType, frontPage.getGlobalQuery())
                .or().like(SfcScrap::getReason, frontPage.getGlobalQuery())
                .or().like(SfcScrap::getCreatedUser, frontPage.getGlobalQuery())
                .or().like(SfcScrap::getModifiedUser, frontPage.getGlobalQuery())
                .or().like(SfcScrap::getDutyUser, frontPage.getGlobalQuery())
    );
        }
        result = sfcScrapService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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

    /**
     * 生产报废检索功能
     * @param site
     * @param sfc
     * @return
     */
    @GetMapping("/getScrapDtoBysfc")
    public R getScrapDtoBysfc(String site,String abnormalNo,String sfc){
        try{
            return R.ok(anomalyService.getScrapDtoBysfc(abnormalNo,site,sfc));
        }catch (Exception e){
            return R.failed("产品报废检索报错:"+e.getMessage());
        }

    }

    /**
     * 生产报废的报废功能
     * @param param
     * @return
     */
    @PostMapping("/scrap")
    public R scrap(@RequestBody HashMap<String,Object> param){
        return R.ok(sfcScrapService.scrap(param));
    }

    /**
     * 导出报废通知单
     * @param shopOrder 工单
     * @param sfc sfc
     * @return
     */
    @GetMapping("/generatorWord")
    public R generatorWord(String shopOrder, String sfc, HttpServletRequest request, HttpServletResponse response){
        sfcScrapService.generatorWord(shopOrder,sfc,response,request);
        return R.ok();
    }
}