package com.ruoyi.web.controller.business;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.*;
import com.ruoyi.business.service.*;
import com.ruoyi.business.utils.OrderCodeFactory;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 退库Controller
 *
 * @author
 * @date 2022-05-29
 */
@Controller
@RequestMapping("/business/returnStock")
public class ReturnStockController extends BaseController
{
    private String prefix = "business/returnStock";

    @Autowired
    private ITOngoodsshelfService tOngoodsshelfService;

    @Autowired
    private ITOngoodsshelfBakService tOngoodsshelfServiceBak;
    @Autowired
    private ITConveyorStatusService tConveyorStatusService;

    @RequiresPermissions("business:returnStock:view")
    @GetMapping()
    public String returnStock(ModelMap mmap)
    {
        // 获取用户
        SysUser sysUser = ShiroUtils.getCurrentUser();
        mmap.put("sysUser", sysUser);
        return prefix + "/returnStock";
    }

    /**
     * 查询入库任务单列表
     */
    @RequiresPermissions("business:returnStock:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TOngoodsshelf tOngoodsshelf)
    {
        startPage();
//        List<TOngoodsshelf> list = tOngoodsshelfService.selectTOngoodsshelfList(tOngoodsshelf);
        List<TOngoodsshelf> list = new ArrayList<>();
        return getDataTable(list);
    }

    @GetMapping(value = "/getBarcode")
    @ResponseBody
    public AjaxResult getbarcode(){
        List<TConveyorStatus> tConveyorStatuses = getTConveyorStatus();
        if(tConveyorStatuses != null && tConveyorStatuses.size() > 0) {
            return success(tConveyorStatuses.get(0));
        }
        logger.error("没有查询到该设备对应的站台信息………………………………");
        return toAjax(false);
    }

    /**
     * 结束入库
     */
    @RequiresPermissions("business:returnStock:add")
    @Log(title = "入库", businessType = BusinessType.INSERT)
    @PostMapping(value = "/finishNoErpIn",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult finishNoErpIn(@RequestBody List<TOngoodsshelf> tOngoodsshelves){
        logger.info("接收到入库请求:{}",tOngoodsshelves);
        if(tOngoodsshelves.size() <= 0){
            logger.error("提交入库的数据为空");
            return error();
        }
        // 入库id
        String lotId = OrderCodeFactory.getOrderCode("TK", "");
        // 创建日期
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        // 获取用户名
        SysUser sysUser = ShiroUtils.getCurrentUser();
        for (TOngoodsshelf s:
                tOngoodsshelves) {
            s.setTasktype("1");
            s.setLotId(lotId);
            s.setOrderId(initOrderId());
            s.setCreateDate(sdf.format(date));
            s.setInstatus("0");
            s.setUserName(sysUser.getUserName());
        }
        return toAjax(tOngoodsshelfService.insertTOngoodsshelfValues(tOngoodsshelves));
    }

    /**
     * 退库记录报表界面
     * @return
     */
    @RequiresPermissions("business:returnStock:view")
    @GetMapping("/report")
    public String report()
    {
        return prefix + "/returnStockReport";
    }

    /**
     * 查询退库任务单列表
     */
    @RequiresPermissions("business:returnStock:list")
    @PostMapping("/report")
    @ResponseBody
    public TableDataInfo reportList(DateCover date)
    {
        startPage();
        List<TOngoodsshelfBak> list = tOngoodsshelfServiceBak.selectTOngoodsshelfBakByCreateDate(
                date.getStartDate(),
                date.getEndDate(), "1", date.getSearch());
        return getDataTable(list);
    }

    /**
     * 导出退库任务单列表
     */
    @RequiresPermissions("business:returnStock:export")
    @Log(title = "导出退库记录报表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DateCover date)
    {
//        tOngoodsshelfBak.setTasktype("1");
//        List<TOngoodsshelfBak> list = tOngoodsshelfServiceBak.selectTOngoodsshelfBakWithoutEmpty(tOngoodsshelfBak);
        List<TOngoodsshelfBak> list = tOngoodsshelfServiceBak.selectTOngoodsshelfBakByCreateDate(
                date.getStartDate(),
                date.getEndDate(), "1", date.getSearch());
        ExcelUtil<TOngoodsshelfBak> util = new ExcelUtil<TOngoodsshelfBak>(TOngoodsshelfBak.class);
        return util.exportExcel(list, "退库记录报表");
    }

    /**
     * 状态说明弹框
     */
    @GetMapping("/stsDescribe")
    public String stsDescribe()
    {
        return prefix + "/stsdescribe";
    }

    /**
     * 生成订单号
     */
    private String initOrderId() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return "SJD" + sdf.format(date) + (int)(Math.random() * 10);
    }

    private List<TConveyorStatus> getTConveyorStatus(){
        SysUser sysUser = ShiroUtils.getCurrentUser();
        String loginIp = ShiroUtils.getIp();
        TConveyorStatus status = new TConveyorStatus();
        status.setPcip(loginIp);
        List<TConveyorStatus> tConveyorStatuses = tConveyorStatusService.selectTConveyorStatusList(status);
        return tConveyorStatuses;
    }
}
