package com.foreverwin.mesnac.common.controller;

import cn.hutool.core.date.DateUtil;
import com.foreverwin.mesnac.common.dto.LabelPrintDto;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.CommonMethods;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mesnac.common.service.PrintLogService;
import com.foreverwin.mesnac.common.model.PrintLog;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Leon.L
 * @since 2021-07-21
 */
@RestController
@RequestMapping("/Z-PRINT-LOG")
public class PrintLogController {

    @Autowired
    public PrintLogService printLogService;

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @PostMapping("")
    public R getPrintLogList(@RequestBody PrintLog printLog){
        List<PrintLog> result;
        try {
            String site = CommonMethods.getSite();
            printLog.setSite(site);

            QueryWrapper<PrintLog> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(printLog);
            result = printLogService.list(queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/getPrintLogListByCondition")
    public R getPrintLogListByCondition(@RequestBody PrintLog printLog){
        List<PrintLog> result;
        try {
            String site = CommonMethods.getSite();
            printLog.setSite(site);
            if (printLog.getStartFromDate_S() != null) {
                printLog.setStartFromDate(DateUtil.parse(printLog.getStartFromDate_S()));
            }
            if (printLog.getStartToDate_S() != null) {
                printLog.setStartToDate(DateUtil.parse(printLog.getStartToDate_S()));
            }
            result = printLogService.getPrintLogListByCondition(printLog);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

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

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

        return R.ok();
    }

    /**
     * 根据sfc，查询当前sfc的打印信息
     * @param sfc
     * @return
     */
    @ResponseBody
    @GetMapping("/findSfcPrintInfoBySfc")
    public R findSfcInfoBySfc(String sfc){
        try {
            return R.ok(printLogService.findSfcPrintInfoBySfc(sfc));
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
    }

    @ResponseBody
    @PostMapping("/sfcLabelPrint")
    public R sfcLabelPrint(@RequestBody Map<String,Object> map) {
        try {
            printLogService.sfcLabelPrint(map);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
        return R.ok();
    }



}