package org.jeecg.modules.gongdandetail.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.gongdandetail.dto.ItdmGongdanDetailEditDTO;
import org.jeecg.modules.gongdandetail.service.IItdmGongdanQuerenService;
import org.jeecg.modules.itdmGongDan.entity.ItdmGongdanDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @Description: 工单detail
 * @Author: jeecg-boot
 * @Date: 2023-05-21
 * @Version: V1.0
 */
@Api(tags = "工单detail")
@RestController
@RequestMapping("/gongdandetail/itdmGongdanDetail")
@Slf4j
public class ItdmGongdanQuerenController extends JeecgController<ItdmGongdanDetail, IItdmGongdanQuerenService> {
    @Autowired
    private IItdmGongdanQuerenService itdmGongdanDetailService;

    /**
     * 分页列表查询
     *
     * @param itdmGongdanDetail
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "工单detail-分页列表查询")
    @ApiOperation(value = "工单detail-分页列表查询", notes = "工单detail-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ItdmGongdanDetail>> queryPageList(String woId, String woNo,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                          HttpServletRequest req) {
        ItdmGongdanDetail itdmGongdanDetail = new ItdmGongdanDetail();
        itdmGongdanDetail.setWoId(woId);


        QueryWrapper<ItdmGongdanDetail> queryWrapper = QueryGenerator.initQueryWrapper(itdmGongdanDetail, req.getParameterMap());
        queryWrapper.inSql(StringUtils.isNotEmpty(woNo) && StringUtils.isEmpty(woId), "wo_id", " select distinct g.id\n" +
                "    from  itdm_test_plan  as t\n" +
                "              inner join itdm_gongdan_master as g on t.id=g.jihua_shiyan_id  where t.weituo_id='" + woNo + "'");
        Page<ItdmGongdanDetail> page = new Page<ItdmGongdanDetail>(pageNo, pageSize);
        IPage<ItdmGongdanDetail> pageList = itdmGongdanDetailService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 编辑
     *
     * @param itdmGongdanDetail
     * @return
     */
    @AutoLog(value = "工单detail-编辑")
    @ApiOperation(value = "工单detail-编辑", notes = "工单detail-编辑")
    //@RequiresPermissions("org.jeecg.modules:itdm_gongdan_detail:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ItdmGongdanDetailEditDTO itdmGongdanDetail) throws ParseException {
        itdmGongdanDetailService.queren(itdmGongdanDetail);
        return Result.OK("编辑成功!");
    }


    @GetMapping(value = "/gongdanIdList")
    public Result<List<String>> gongdanIdList(String weituoId) {
        return Result.OK(itdmGongdanDetailService.getByweituoId(weituoId));
    }

    @GetMapping(value = "/gongdanWorkdateList")
    public Result<List<String>> getWorkDateByWeituoNO(String weituoId) {
        return Result.OK(itdmGongdanDetailService.getWorkDateByWeituoNO(weituoId));
    }


    @GetMapping(value = "/gongdanWoIdList")
    public Result<List<String>> getWoIdByWeituoIdAndWordDate(String weituoId, String riqi) {
        return Result.OK(itdmGongdanDetailService.getWoIdByWeituoIdAndWordDate(weituoId, riqi));
    }


    @GetMapping(value = "/gongdanDateListByWoId")
    @ApiOperation(value = "工单detail-获取试验类型", notes = "工单detail-获取试验类型")

    public Result<List<Map<String, String>>> gongdanDateList(String woId, String riqi) {
        List<Map<String, String>> detailListByWoId = itdmGongdanDetailService.getDetailListByWoId(woId, riqi);
        return Result.OK(detailListByWoId);
    }


    @GetMapping(value = "/gongdanShebeiListByWoId")
    @ApiOperation(value = "工单detail-获取试验设备", notes = "工单detail-获取试验设备")

    public Result<List<Map<String, String>>> gongdanShebeiList(String woId, String riqi, String shiyanType) {
        List<Map<String, String>> detailListByWoId = itdmGongdanDetailService.gongdanShebeiList(woId, riqi, shiyanType);
        return Result.OK(detailListByWoId);
    }


    @GetMapping(value = "/gongdanDateListByWoId1")
    @ApiOperation(value = "工单detail-获取试验时间", notes = "工单detail-获取试验时间")

    public Result<Map<String, String>> gongdanDateList1(String woId, String riqi, String shiyanType, String shebeiId) {

        Map<String, String> detailListByWoId = itdmGongdanDetailService.getDetailListByWoId1(woId, riqi, shiyanType, shebeiId);
        return Result.OK(detailListByWoId);
    }


}
