package com.sw.mes.fb.turnstore.controller;

import com.sw.mes.core.response.ResponsePage;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.core.utils.EasyExcelUtils;
import com.sw.mes.core.utils.PageUtil;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.fb.turnstore.dto.ReturnDetailsOfSquareRodDto;
import com.sw.mes.fb.turnstore.service.RawMaterialReceivingDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author wanghao
 * @Date: 2022/6/2
 * @Modified By:
 */
@RestController
@RequestMapping("/returnDetailsOfSquareRod")
@Slf4j
@Api(description = "方棒退库明细")
public class ReturnDetailsOfSquareRodController {

    @Autowired
    private RawMaterialReceivingDetailService rawMaterialReceivingDetailService;

    @ApiOperation(value = "方棒退库明细报表")
    @ApiImplicitParams({@ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_crystalNo", value = "晶段编码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_GTE_confirmTime", value = "确认开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LT_confirmTime", value = "确认结束时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_quitType", value = "退库类型", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_state", value = "状态", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_GTE_time", value = "操作开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LT_time", value = "操作结束时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_outOrderNo", value = "退库单号", dataType = "string", paramType = "query")})
    @GetMapping("/getListPage")
    public Result<ResponsePage<ReturnDetailsOfSquareRodDto>> getCutRecordListPage(String search_EQ_crystalNo, String search_GTE_confirmTime, String search_LT_confirmTime,
                                                                                  String search_EQ_quitType, String search_EQ_state, String search_GTE_time,
                                                                                  String search_LT_time,String search_EQ_outOrderNo,
                                                                                  @ApiIgnore RequestPage requestPage) {
        requestPage.setOrderField(null);
        search_EQ_crystalNo = StringUtils.isBlank(search_EQ_crystalNo) ? null : search_EQ_crystalNo;
        search_GTE_confirmTime = StringUtils.isBlank(search_GTE_confirmTime) ? null : search_GTE_confirmTime;
        search_LT_confirmTime = StringUtils.isBlank(search_LT_confirmTime) ? null : search_LT_confirmTime;
        search_EQ_quitType = StringUtils.isBlank(search_EQ_quitType) ? null : search_EQ_quitType;
        search_EQ_state = StringUtils.isBlank(search_EQ_state) ? null : search_EQ_state;
        search_GTE_time = StringUtils.isBlank(search_GTE_time) ? null : search_GTE_time;
        search_LT_time = StringUtils.isBlank(search_LT_time) ? null : search_LT_time;
        search_EQ_outOrderNo = StringUtils.isBlank(search_EQ_outOrderNo) ? null : search_EQ_outOrderNo;
//        if (StringUtils.isBlank(search_LIKE_crystalNo)) search_LIKE_crystalNo = "";
//        Date currentTime = new Date();
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String str = formatter.format(currentTime);
//        if (StringUtils.isBlank(search_GTE_confirmTime)) search_GTE_confirmTime = "2021-01-11 13:19:51";
//        if (StringUtils.isBlank(search_LT_confirmTime)) search_LT_confirmTime = str;
        ResponsePage<ReturnDetailsOfSquareRodDto> responsePage = rawMaterialReceivingDetailService.getListPageInfo(search_EQ_crystalNo, search_GTE_confirmTime, search_LT_confirmTime,
                search_EQ_quitType,search_EQ_state,search_GTE_time,search_LT_time,search_EQ_outOrderNo,
                PageUtil.createPageable(requestPage));
        return ResultGenerator.success(responsePage);
    }

    @ApiOperation(value = "方棒退库明细报表导出")
    @ApiImplicitParams({@ApiImplicitParam(name = "rows", value = "行数", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码", dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "search_LIKE_crystalNo", value = "晶段编码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_GTE_confirmTime", value = "操作开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LT_confirmTime", value = "操作结束时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_quitType", value = "退库类型", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_state", value = "状态", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_GTE_time", value = "操作开始时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_LT_time", value = "操作结束时间", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "search_EQ_outOrderNo", value = "退库单号", dataType = "string", paramType = "query")})
    @GetMapping("/export")
    public void cutExport(String search_EQ_crystalNo, String search_GTE_confirmTime, String search_LT_confirmTime,
                          String search_EQ_quitType, String search_EQ_state, String search_GTE_time,
                          String search_LT_time,String search_EQ_outOrderNo,
                          @ApiIgnore RequestPage requestPage, HttpServletResponse response) {
        try {
            search_EQ_crystalNo = StringUtils.isBlank(search_EQ_crystalNo) ? null : search_EQ_crystalNo;
            search_GTE_confirmTime = StringUtils.isBlank(search_GTE_confirmTime) ? null : search_GTE_confirmTime;
            search_LT_confirmTime = StringUtils.isBlank(search_LT_confirmTime) ? null : search_LT_confirmTime;
            search_EQ_quitType = StringUtils.isBlank(search_EQ_quitType) ? null : search_EQ_quitType;
            search_EQ_state = StringUtils.isBlank(search_EQ_state) ? null : search_EQ_state;
            search_GTE_time = StringUtils.isBlank(search_GTE_time) ? null : search_GTE_time;
            search_LT_time = StringUtils.isBlank(search_LT_time) ? null : search_LT_time;
            search_EQ_outOrderNo = StringUtils.isBlank(search_EQ_outOrderNo) ? null : search_EQ_outOrderNo;
//            if (StringUtils.isBlank(search_LIKE_crystalNo)) search_LIKE_crystalNo = "";
//            Date currentTime = new Date();
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            String str = formatter.format(currentTime);
//            if (StringUtils.isBlank(search_GTE_confirmTime)) search_GTE_confirmTime = "2021-01-11 13:19:51";
//            if (StringUtils.isBlank(search_LT_confirmTime)) search_LT_confirmTime = str;
            List<ReturnDetailsOfSquareRodDto> list1 = rawMaterialReceivingDetailService.getListInfo(search_EQ_crystalNo, search_GTE_confirmTime, search_LT_confirmTime,
                    search_EQ_quitType,search_EQ_state,search_GTE_time,search_LT_time,search_EQ_outOrderNo);
            EasyExcelUtils.write(response, ReturnDetailsOfSquareRodDto.class, "方棒退库明细", "方棒退库明细", list1);
        } catch (Exception e) {
            throw new RuntimeException("方棒退库明细导出失败，错误信息：" + e.getMessage());
        }
    }
}
