package com.zbf.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbf.system.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.zbf.common.annotation.Log;
import com.zbf.common.core.controller.BaseController;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.core.domain.model.LoginUser;
import com.zbf.common.core.page.TableDataInfo;
import com.zbf.common.enums.BusinessType;
import com.zbf.common.utils.OrderCodeFactory;
import com.zbf.common.utils.SecurityUtils;
import com.zbf.common.utils.poi.ExcelUtil;
import com.zbf.system.print.SalesTicket;
import com.zbf.system.print.YcPrinter;
import com.zbf.system.service.ITRkWareNoticeService;
import com.zbf.web.controller.section.EnhanceDataList;

/**
 * 入库通知单Controller
 *
 * @author tony
 * @date 2024-06-18
 */
@RestController
@RequestMapping("/system/trkNotice")
public class TRkWareNoticeController extends BaseController {
    @Autowired
    private ITRkWareNoticeService tRkWareNoticeService;

    /**
     * 查询入库通知单列表
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:list')")
    @GetMapping("/list")
    @EnhanceDataList(entityType = TRkWareNotice.class)
    public TableDataInfo list(TRkWareNotice tRkWareNotice) {
        startPage();
        tRkWareNotice.setDeptId(getDeptId());
        List<TRkWareNotice> list = tRkWareNoticeService.selectTRkWareNoticeList(tRkWareNotice);
        return getDataTable(list);
    }

    /**
     * 导出入库通知单列表
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:export')")
    @Log(title = "入库通知单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TRkWareNotice tRkWareNotice) {
        tRkWareNotice.setDeptId(getDeptId());
        List<TRkWareNotice> list = tRkWareNoticeService.exportTRkWareNoticeList(tRkWareNotice);
        ExcelUtil<TRkWareNotice> util = new ExcelUtil<TRkWareNotice>(TRkWareNotice.class);
        util.exportExcel(response, list, "入库通知单数据");
    }

    /**
     * 获取入库通知单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        TRkWareNotice tRkWareNotice = tRkWareNoticeService.selectTRkWareNoticeById(id);
        if (tRkWareNotice == null) {
            tRkWareNotice = new TRkWareNotice();
            List<TRkWareNoticeTab> tabs = new ArrayList<TRkWareNoticeTab>();
            tRkWareNotice.setTRkWareNoticeTabList(tabs);
        }
        return success(tRkWareNotice);
    }

    /**
     * 获取入库通知单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:query')")
    @GetMapping(value = "/noticeId/{id}")
    public AjaxResult getNoticeInfo(@PathVariable("id") String id) {
        TRkWareNotice tRkWareNotice = tRkWareNoticeService.selectTRkWareNoticeByDeliveryId(id);
        if (tRkWareNotice == null) {
            tRkWareNotice = new TRkWareNotice();
            List<TRkWareNoticeTab> tabs = new ArrayList<TRkWareNoticeTab>();
            tRkWareNotice.setTRkWareNoticeTabList(tabs);
        }
        return success(tRkWareNotice);
    }

    /**
     * 新增入库通知单
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:add')")
    @Log(title = "入库通知单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TRkWareNotice tRkWareNotice) {
        tRkWareNotice.setDeptId(getDeptId());
        tRkWareNotice.setId(OrderCodeFactory.getOrderCode("", ""));
        tRkWareNotice.setNoticeId(OrderCodeFactory.getOrderCode("TWO", ""));
        tRkWareNotice.setApplicant(SecurityUtils.getUserNick());
        tRkWareNotice.setCreateBy(getUsername());
        tRkWareNotice.setReceivingStatus("0");
        return toAjax(tRkWareNoticeService.insertTRkWareNotice(tRkWareNotice));
    }

    /**
     * 修改入库通知单
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:edit')")
    @Log(title = "入库通知单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TRkWareNotice tRkWareNotice) {
        return toAjax(tRkWareNoticeService.updateTRkWareNotice(tRkWareNotice));
    }

    /**
     * 删除入库通知单
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:remove')")
    @Log(title = "入库通知单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult removeTabs(@PathVariable String[] ids) {
        return toAjax(tRkWareNoticeService.deleteTRkWareNoticeByIds(ids));
    }

    /**
     * 删除入库通知单明细
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:remove')")
    @Log(title = "入库通知单", businessType = BusinessType.DELETE)
    @DeleteMapping("/tabs/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        Map<String, String> result = tRkWareNoticeService.deleteTRkWareNoticeTabByIds(ids);
        return success(result);
    }

    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:remove')")
    @Log(title = "入库通知单", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable String[] ids) {
        return toAjax(tRkWareNoticeService.disabledTRkWareNoticeByIds(ids));
    }

    /**
     * 禁用
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:edit')")
    @Log(title = "入库通知单", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable String[] ids) {
        return toAjax(tRkWareNoticeService.enableTRkWareNoticeByIds(ids));
    }

    /**
     * 修改状态
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:edit')")
    @Log(title = "入库通知单", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TRkWareNotice tRkWareNotice) {
        return toAjax(tRkWareNoticeService.updateStatus(tRkWareNotice));
    }

    /**
     * 收货接口
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:add')")
    @Log(title = "确认入库通知单明细", businessType = BusinessType.INSERT)
    @PostMapping("/confirmNoticeTab")
    public AjaxResult confirmNoticeTab(@RequestBody TRkWareNoticeTabUpdate update) {
        update.setDeptId(getDeptId());
        update.setUserName(getLoginUser().getUser().getNickName());
        Map<String, String> result = tRkWareNoticeService.confirmNoticeTab(update);
        return success(result);
    }

    /**
     * 入库通知单明细保存
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:add')")
    @Log(title = "保存入库通知单明细", businessType = BusinessType.INSERT)
    @PostMapping("/saveNoticeTab")
    public AjaxResult saveNoticeTab(@RequestBody TRkWareNoticeTabUpdate update) {
        update.setDeptId(getDeptId());
        update.setUserName(getLoginUser().getUser().getNickName());
        Map<String, String> result = tRkWareNoticeService.saveNoticeTab(update);
        return success(result);
    }

    /**
     * 入库通知单明细手动完成
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:add')")
    @Log(title = "手动完成入库通知单明细", businessType = BusinessType.INSERT)
    @PostMapping("/manualCompletion")
    public AjaxResult manualCompletion(@RequestBody TRkWareNoticeTabUpdate update) {
        update.setDeptId(getDeptId());
        update.setUserName(getLoginUser().getUser().getNickName());
        Map<String, String> result = tRkWareNoticeService.manualCompletion(update);
        return success(result);
    }

    /**
     * 上架入库
     */
    @PreAuthorize("@ss.hasPermi('system:trkNotice:add')")
    @Log(title = "入库任务上架入库", businessType = BusinessType.INSERT)
    @PostMapping("/shelvesConfirm")
    public AjaxResult shelvesConfirm(@RequestBody TRkWareNoticeTabUpdate update) {
        update.setDeptId(getDeptId());
        update.setUserName(getLoginUser().getUser().getNickName());
        Map<String, String> result = tRkWareNoticeService.shelvesConfirm(update);
        return success(result);
    }
    /**
     * 导入
     */
    @Log(title = "入库数据", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TRkWareNoticeTab> util = new ExcelUtil<TRkWareNoticeTab>(TRkWareNoticeTab.class);
        List<TRkWareNoticeTab> trList = util.importExcel(file.getInputStream());
        LoginUser loginUser = getLoginUser();
        String operName = loginUser.getUser().getNickName();
        String message = tRkWareNoticeService.importData(trList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 一级库导入
     */
    @Log(title = "入库数据", businessType = BusinessType.IMPORT)
    @PostMapping("/importExcelData")
    public AjaxResult importExcelData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TRkWareNoticeTabExcel> util = new ExcelUtil<TRkWareNoticeTabExcel>(TRkWareNoticeTabExcel.class);
        List<TRkWareNoticeTabExcel> trList = util.importExcel(file.getInputStream());
        LoginUser loginUser = getLoginUser();
        String operName = loginUser.getUser().getNickName();
        String message = tRkWareNoticeService.importExcelData(trList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TRkWareNoticeTab> util = new ExcelUtil<TRkWareNoticeTab>(TRkWareNoticeTab.class);
        util.importTemplateExcel(response,"入库通知单");
    }

    @PostMapping("/importExcelTemplate")
    public void importExcelTemplate(HttpServletResponse response)
    {
        ExcelUtil<TRkWareNoticeTabExcel> util = new ExcelUtil<TRkWareNoticeTabExcel>(TRkWareNoticeTabExcel.class);
        util.importTemplateExcel(response,"一级入库通知单");
    }

    @Log(title = "入库单条码打印", businessType = BusinessType.INSERT)
    @PostMapping("/printBarCode")
    public AjaxResult printBarCode(@RequestBody TRkWareNoticeTabUpdate update) {
        int count = update.getPrintNum();
        for (int i = 0; i < count; i++) {
            SalesTicket stk = new SalesTicket(update,null);
            YcPrinter p = new YcPrinter(stk);
            p.printer();
        }

        return AjaxResult.success("打印成功");
    }

     /**
      * 发送入库通知单消息
      */
     @Log(title = "发送入库通知单消息", businessType = BusinessType.UPDATE)
     @PostMapping("/sendOrderNotice")
     public AjaxResult sendNotice(@RequestBody TRkWareNoticeTabUpdate update) {
         update.setCreateBy(getLoginUser().getUser().getNickName());
         update.setDeptId(getDeptId());
         return AjaxResult.success(tRkWareNoticeService.sendOrderNotice(update));
     }


    @Log(title = "推送入库通知单", businessType = BusinessType.UPDATE)
    @PostMapping("/sendIwms")
    public AjaxResult sendIwms(@RequestBody TRkWareNoticeTabUpdate update) {
        update.setCreateBy(getLoginUser().getUser().getNickName());
        update.setDeptId(getDeptId());
        return AjaxResult.success(tRkWareNoticeService.sendIwmsUnified(update));
    }



    /**
     * 手动入库
     */
    @Log(title = "无单收货", businessType = BusinessType.UPDATE)
    @PostMapping("/handAddGoodsTab")
    public AjaxResult handAddGoodsTab(@RequestBody TRkWareNoticeTabUpdate update) {
        update.setId(OrderCodeFactory.getOrderCode("", ""));
        update.setNoticeId(OrderCodeFactory.getOrderCode("TWO", ""));
        return AjaxResult.success(tRkWareNoticeService.handAddGoodsTab(update));
    }


    /**
     * 虚拟库位上架
     */
    @Log(title = "虚拟库位上架", businessType = BusinessType.UPDATE)
    @PostMapping("/virtualListing")
    public AjaxResult virtualListing(@RequestBody TCkOrders orders) {
        return AjaxResult.success(tRkWareNoticeService.virtualListing(orders));
    }

    /**
     * 车间退货入库
     */
    @Log(title = "车间退货入库", businessType = BusinessType.UPDATE)
    @PostMapping("/workshopReturn")
    public AjaxResult workshopReturn(@RequestBody TckOrdersBak orders) {
        orders.setApplicant(getLoginUser().getUser().getNickName());
        orders.setRecordPerson(getLoginUser().getUser().getNickName());
        orders.setDeptId(getDeptId());
        return AjaxResult.success(tRkWareNoticeService.workshopReturn(orders));
    }
}
