package com.ruoyi.web.controller.nanjing;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.nanjing.domain.TBdSubstation;
import com.ruoyi.nanjing.service.ITBdSubstationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.nanjing.domain.TRpWorktrayhistoryinfo;
import com.ruoyi.nanjing.service.ITRpWorktrayhistoryinfoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 工位历史生产信息Controller
 * 
 * @author limy
 * @date 2021-01-28
 */
@Controller
@RequestMapping("/nanjing/WorkTrayHistoryInfo")
public class TRpWorktrayhistoryinfoController extends BaseController
{
    private String prefix = "nanjing/WorkTrayHistoryInfo";

    TRpWorktrayhistoryinfo thi = new TRpWorktrayhistoryinfo();
    @Autowired
    private ITRpWorktrayhistoryinfoService tRpWorktrayhistoryinfoService;
    @Autowired
    private ITBdSubstationService substationService;
    @RequiresPermissions("nanjing:WorkTrayHistoryInfo:view")
    @GetMapping()
    public String WorkTrayHistoryInfo(ModelMap map)
    {
        List<TBdSubstation> substations = substationService.selectTBdSubstationList(new TBdSubstation());
        map.addAttribute("list",substations);
        return prefix + "/WorkTrayHistoryInfo";
    }

    /**
     * 查询工位历史生产信息列表
     */
    @RequiresPermissions("nanjing:WorkTrayHistoryInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TRpWorktrayhistoryinfo t1) throws Exception {
        startPage();
        List<TRpWorktrayhistoryinfo> list = tRpWorktrayhistoryinfoService.selectTRpWorktrayhistoryinfoList(t1);
        return getDataTable(list);
    }


    /**
     * 查询数据，动态表及参数
     * @param t1
     * @param request
     * @return
     * @throws ParseException
     */
    @PostMapping("/searchPara")
    @ResponseBody
    public TableDataInfo searchPara(TRpWorktrayhistoryinfo t1, HttpServletRequest request ) throws ParseException {
        startPage();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
        TRpWorktrayhistoryinfo tRpWorktrayhistoryinfo = new TRpWorktrayhistoryinfo();
        String bt = request.getParameter("beginTime");
        String et = request.getParameter("endTime");
        if(!(bt.equals(""))&&bt!=null){
            tRpWorktrayhistoryinfo.setBeginTime(bt);
        }else if (!(et.equals(""))&&et!=null){
            tRpWorktrayhistoryinfo.setEndTime(et);
        }
        else
        {
            String dt = simpleDateFormat.format(new Date());
            tRpWorktrayhistoryinfo.setBeginTime("1975-01-01 00:00:000");
            tRpWorktrayhistoryinfo.setEndTime(dt);
        }
        TBdSubstation substation = new TBdSubstation();
        substation.setTableName(t1.getSubstation().getTableName());
        tRpWorktrayhistoryinfo.setSubstation(substation);
        tRpWorktrayhistoryinfo.setRfidNO(request.getParameter("rfidNO"));
        List<TRpWorktrayhistoryinfo> list = tRpWorktrayhistoryinfoService.selectTRpWorktrayhistoryinfoList(tRpWorktrayhistoryinfo);
        return getDataTable(list);
    }
    /**
     * 导出工位历史生产信息列表
     */
    @RequiresPermissions("nanjing:WorkTrayHistoryInfo:export")
    @Log(title = "工位历史生产信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TRpWorktrayhistoryinfo t1,HttpServletRequest request)
    {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//注意月份是MM
        TRpWorktrayhistoryinfo tRpWorktrayhistoryinfo = new TRpWorktrayhistoryinfo();
        String bt = request.getParameter("beginTime");
        String et = request.getParameter("endTime");
        if(!(bt.equals(""))&&bt!=null){
            tRpWorktrayhistoryinfo.setBeginTime(bt);
        }else if (!(et.equals(""))&&et!=null){
            tRpWorktrayhistoryinfo.setEndTime(et);
        }
        else
        {
            String dt = simpleDateFormat.format(new Date());
            tRpWorktrayhistoryinfo.setBeginTime("1975-01-01 00:00:000");
            tRpWorktrayhistoryinfo.setEndTime(dt);
        }
        TBdSubstation substation = new TBdSubstation();
        substation.setTableName(t1.getSubstation().getTableName());
        tRpWorktrayhistoryinfo.setSubstation(substation);
        tRpWorktrayhistoryinfo.setRfidNO(request.getParameter("rfidNO"));
        List<TRpWorktrayhistoryinfo> list = tRpWorktrayhistoryinfoService.selectTRpWorktrayhistoryinfoList(tRpWorktrayhistoryinfo);
        for (TRpWorktrayhistoryinfo th: list) {
            if(th.getSubstation()!=null&&th.getSubstation().getStationCode()!=null){
                th.setStationCode(th.getSubstation().getStationCode());
            }if(th.getStationpara()!=null&&th.getStationpara().getRfidNO()!=null){
                th.setRfidNO(th.getStationpara().getRfidNO());
            }if( th.getStationpara()!=null&&th.getStationpara().getBarcode()!=null){
                th.setBarCode(th.getStationpara().getBarcode());
            }if(th.getBarcoderelationship()!=null&&th.getBarcoderelationship().getProductBarcode()!=null){
                th.setProductBarcode(th.getBarcoderelationship().getProductBarcode());
            }if( th.getStationpara()!=null&&th.getStationpara().getState()!=null){
                th.setState(th.getStationpara().getState());
            }if( th.getStationpara()!=null&& th.getStationpara().getInsertTime()!=null){
                th.setInsertTime(th.getStationpara().getInsertTime());
            }
        }
        ExcelUtil<TRpWorktrayhistoryinfo> util = new ExcelUtil<TRpWorktrayhistoryinfo>(TRpWorktrayhistoryinfo.class);
        return util.exportExcel(list, "WorkTrayHistoryInfo");
    }
}
