package com.foreverwin.mesnac.quality.controller;

import cn.hutool.core.date.DateUtil;
import com.foreverwin.mesnac.common.service.InspectionTaskService;
import com.foreverwin.mesnac.quality.dto.InspectionTaskManageRequest;
import com.foreverwin.mesnac.quality.model.InspectionTaskManage;
import com.foreverwin.mesnac.quality.service.InspectionTaskManageService;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


@RestController
@RequestMapping("/InspectionTaskManage")
public class InspectionTaskManageController {

    @Autowired
    private InspectionTaskManageService inspectionTaskManageService;

    @ResponseBody
    @PostMapping("/findInspectionTask")
    public R findInspectionTask(@RequestBody InspectionTaskManageRequest inspectionTaskManageRequest) {
        List<InspectionTaskManage> result;

        try {
            String site = CommonMethods.getSite();
            inspectionTaskManageRequest.setSite(site);

            if (inspectionTaskManageRequest.getDateFromStr() != null) {
                inspectionTaskManageRequest.setDateFrom(DateUtil.parse(inspectionTaskManageRequest.getDateFromStr()));
            }
            if (inspectionTaskManageRequest.getDateToStr() != null) {
                inspectionTaskManageRequest.setDateTo(DateUtil.parse(inspectionTaskManageRequest.getDateToStr()));
            }
            result = inspectionTaskManageService.findInspectionTask(inspectionTaskManageRequest);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

    @ResponseBody
    @PostMapping("/findInspectionTaskDetail")
    public R findInspectionTaskDetail(@RequestBody InspectionTaskManageRequest inspectionTaskManageRequest) {
        InspectionTaskManage result;

        try {
            String site = CommonMethods.getSite();
            inspectionTaskManageRequest.setSite(site);

            result = inspectionTaskManageService.findInspectionTaskAndDetail(inspectionTaskManageRequest);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

    @ResponseBody
    @PostMapping("/completeInspectionTask")
    public R completeInspectionTask(@RequestBody List<InspectionTaskManageRequest> requestList) {
        try {
            if (requestList == null || requestList.size() <= 0) {
                throw BusinessException.build("请选择至少一笔记录进行操作！");
            }

            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            inspectionTaskManageService.completeInspectionTask(site, user, requestList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok();
    }

    @ResponseBody
    @PostMapping("/saveInspectionTaskDetail")
    public R saveInspectionTaskDetail(@RequestBody InspectionTaskManage inspectionTaskManage) {
        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            inspectionTaskManageService.saveInspectionTaskDetail(site, user, inspectionTaskManage);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok();
    }

    @Autowired
    private InspectionTaskService inspectionTaskService;

    @ResponseBody
    @GetMapping("/createTask")
    public R createTask(String category, String sfc, String shopOrder, String operation, String stepId) {
        try {
            CommonMethods.setSite("1000");

            Map<String, Object> map = new HashMap<>();
            map.put("CATEGORY", category);
            map.put("SFC", sfc);
            map.put("SHOP_ORDER", shopOrder);
            map.put("OPERATION", operation);
            map.put("STEP_ID", stepId);
            inspectionTaskService.createTask(map);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok();
    }
}
