package com.foreverwin.mesnac.quality.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.common.constant.Constants;
import com.foreverwin.mesnac.quality.mapper.SelfReportMapper;
import com.foreverwin.mesnac.quality.model.SelfReport;
import com.foreverwin.mesnac.quality.service.SelfReportService;
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.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 * @author Philip
 * @since 2021-08-20
 */
@RestController
@RequestMapping("/Z-SELF-REPORT")
public class SelfReportController {

    @Autowired
    public SelfReportService selfReportService;

    @Autowired
    public SelfReportMapper selfReportMapper;
    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/doAdd")
    public R doAdd(@RequestBody Map<String,Object> map){
        selfReportService.doAdd(map);
        return R.ok();
    }
    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getSelfReportById(@PathVariable String id) {
        return R.ok( selfReportService.getById(id));
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/selectList")
    public R getSelfReportList(SelfReport selfReport){
        List<SelfReport> result;
        QueryWrapper<SelfReport> queryWrapper = new QueryWrapper<>();
        selfReport.setSite(CommonMethods.getSite());
        queryWrapper.setEntity(selfReport);
        result = selfReportService.selectList(queryWrapper, LocaleContextHolder.getLocale().getLanguage(),selfReport.getStartTime(),selfReport.getEndTime());
        return R.ok(result);
    }
    @ResponseBody
    @RequestMapping("/reject")
    public R updateById(String handle){
        return R.ok(selfReportService.updateState(handle, Constants.STATE_REJECT));
    }
    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<SelfReport> frontPage, SelfReport selfReport){
        IPage result;
        QueryWrapper<SelfReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(selfReport);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(SelfReport::getHandle, frontPage.getGlobalQuery())
                .or().like(SelfReport::getSite, frontPage.getGlobalQuery())
                .or().like(SelfReport::getTaskNo, frontPage.getGlobalQuery())
                .or().like(SelfReport::getWorkCenter, frontPage.getGlobalQuery())
                .or().like(SelfReport::getShopOrder, frontPage.getGlobalQuery())
                .or().like(SelfReport::getItem, frontPage.getGlobalQuery())
                .or().like(SelfReport::getOpStep, frontPage.getGlobalQuery())
                .or().like(SelfReport::getResrce, frontPage.getGlobalQuery())
                .or().like(SelfReport::getSfc, frontPage.getGlobalQuery())
                .or().like(SelfReport::getNcCode, frontPage.getGlobalQuery())
                .or().like(SelfReport::getLocation, frontPage.getGlobalQuery())
                .or().like(SelfReport::getState, frontPage.getGlobalQuery())
                .or().like(SelfReport::getRemark, frontPage.getGlobalQuery())
                .or().like(SelfReport::getCreateUser, frontPage.getGlobalQuery())
                .or().like(SelfReport::getModifyUser, frontPage.getGlobalQuery())
                .or().like(SelfReport::getNcCodeDesc, frontPage.getGlobalQuery())
                .or().like(SelfReport::getReportUserGroup, frontPage.getGlobalQuery())
    );
        }
        result = selfReportService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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


    /**
     *  查询设备和设备上的检验人员
     * @param
     * @return
     */
    @GetMapping("/selectResourceAndCheckUsr")
    public R selectResourceAndCheckUsr() {
        List<SelfReport> result;
        try {
            String site = CommonMethods.getSite();
            result = selfReportMapper.selectResourceAndCheckUsr(site);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }


}