package com.foreverwin.mesnac.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.mapper.InspectionItemAdditionMapper;
import com.foreverwin.mesnac.common.mapper.InspectionItemDetailMapper;
import com.foreverwin.mesnac.common.model.InspectionItem;
import com.foreverwin.mesnac.common.model.InspectionItemAddition;
import com.foreverwin.mesnac.common.model.InspectionItemDetail;
import com.foreverwin.mesnac.common.service.InspectionItemService;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.R;
import com.visiprise.common.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 *
 * @author Philip
 * @since 2021-06-09
 */
@RestController
@RequestMapping("/Z-INSPECTION-ITEM")
public class InspectionItemController {

    @Autowired
    public InspectionItemService inspectionItemService;

    @Autowired
    public InspectionItemDetailMapper inspectionItemDetailMapper;

    @Autowired
    public InspectionItemAdditionMapper inspectionItemAdditionMapper;

    @PostMapping("/deleteById")
    public R deleteById(@RequestBody InspectionItem inspectionItem){
       inspectionItemService.deleteServece(inspectionItem);
        return R.ok();
    }
    @PostMapping("/saveAll")
    public R saveAll(@RequestBody InspectionItem inspectionItem){
        return R.ok(inspectionItemService.saveAll(inspectionItem));
    }
    @GetMapping("/queryAll")
    public R queryAll(InspectionItem inspectionItem){
        if(StringUtil.isBlank(inspectionItem.getInspectionItemNo())){
            throw new BaseException("请输入检验项目编号版本号");
        }
        if(StringUtil.isEmpty(inspectionItem.getRevision())){
            String revision = inspectionItemService.findRevisionByCurrentRevision(inspectionItem.getInspectionItemNo());
            if(StringUtil.isEmpty(revision)){
                throw new BaseException("请输入版本号");
            }
            inspectionItem.setRevision(revision);
        }
        inspectionItem.setSite(CommonMethods.getSite());
        inspectionItem.setHandle(HandleEnum.INSPECTION_ITEM.getHandle(inspectionItem.getSite(),inspectionItem.getInspectionItemNo(),inspectionItem.getRevision()));

        InspectionItem handle = inspectionItemService.getById(inspectionItem.getHandle());
        if(null == handle){
            throw new BaseException("根据此项目编号以及版本未检索到数据");
        }

        HashMap<String, Object> findCondition = new HashMap<>(2);
        findCondition.put(InspectionItemDetail.INSPECTION_ITEM_BO,inspectionItem.getHandle());
        List<InspectionItemDetail> inspectionItemDetails = inspectionItemDetailMapper.selectByMap(findCondition);
        List<InspectionItemAddition> inspectionItemAdditions = inspectionItemAdditionMapper.selectByMap(findCondition);

        handle.setInspectionItemDetailList(inspectionItemDetails);
        handle.setInspectionItemAdditionList(inspectionItemAdditions);

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<InspectionItem> frontPage, InspectionItem inspectionItem){
        IPage result;
        QueryWrapper<InspectionItem> queryWrapper = new QueryWrapper<>();
        inspectionItem.setSite(CommonMethods.getSite());
        queryWrapper.setEntity(inspectionItem);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                    .like(InspectionItem::getInspectionItemNo, frontPage.getGlobalQuery())
                .or().like(InspectionItem::getInspectionType, frontPage.getGlobalQuery())
                .or().like(InspectionItem::getRevision, frontPage.getGlobalQuery())
                .or().like(InspectionItem::getCurrentRevision, frontPage.getGlobalQuery())
                .or().like(InspectionItem::getDescription, frontPage.getGlobalQuery())
                .or().like(InspectionItem::getStatus, frontPage.getGlobalQuery())
    );
        }
        result = inspectionItemService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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