package com.zbf.web.controller.system;

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.poi.ExcelUtil;
import com.zbf.system.domain.TCkOrderdetail;
import com.zbf.system.domain.TCkOrders;
import com.zbf.system.domain.TRkWareNotice;
import com.zbf.system.domain.vo.TCkOrdersVo;
import com.zbf.system.service.ITCkOrdersService;
import com.zbf.web.controller.section.EnhanceDataList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出库Controller
 *
 * @author tony
 * @date 2024-06-24
 */
@RestController
@RequestMapping("/system/ckOrders")
public class TCkOrdersController extends BaseController {
    @Autowired
    private ITCkOrdersService tCkOrdersService;

    /**
     * 查询出库列表
     */
    //@PreAuthorize("@ss.hasPermi('system:ckOrders:list')")
    @GetMapping("/list")
    @EnhanceDataList(entityType = TCkOrders.class)
    public TableDataInfo list(TCkOrders tCkOrders) {
        startPage();
        List<TCkOrders> list = tCkOrdersService.selectTCkOrdersList(tCkOrders);
        return getDataTable(list);
    }

    /**
     * 导出出库列表
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:export')")
    @Log(title = "出库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCkOrders tCkOrders) {
        List<TCkOrders> list = tCkOrdersService.exportTCkOrdersList(tCkOrders);
        ExcelUtil<TCkOrders> util = new ExcelUtil<TCkOrders>(TCkOrders.class);
        util.exportExcel(response, list, "出库数据");
    }

    /**
     * 获取出库详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:ckOrders:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
//        TCkOrders tCkOrders = tCkOrdersService.selectTCkOrdersById(id);
        return success(tCkOrdersService.selectTCkOrdersById(id));
    }

    /**
     * 获取出库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:query')")
    @GetMapping(value = "/order/{id}")
    public AjaxResult getOrderInfo(@PathVariable("id") String id) {
        return success(tCkOrdersService.selectTCkOrdersByDeliveryId(id));
    }

    /**
     * 新增出库
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:add')")
    @Log(title = "新增出库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCkOrders tCkOrders) {
        tCkOrders.setId(OrderCodeFactory.getOrderCode("", ""));
        tCkOrders.setOrderId(OrderCodeFactory.getOrderCode("CK", ""));
        tCkOrders.setDeptId(getDeptId());

        tCkOrders.setRecordDate(new Date());
        return toAjax(tCkOrdersService.insertTCkOrders(tCkOrders));
    }


    /**
     * 新增出库明细
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:add')")
    @Log(title = "出库", businessType = BusinessType.INSERT)
    @PostMapping("/updateTckOrderDetails")
    public AjaxResult updateTckOrderDetails(@RequestBody List<TCkOrderdetail> tCkOrderdetails) {
        return toAjax(tCkOrdersService.insertTCkOrdersDetails(tCkOrderdetails));
    }

    /**
     * 调拨创建
     */
    @Log(title = "调拨创建", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public AjaxResult create(@RequestBody TCkOrdersVo tCkOrders) {
        return toAjax(tCkOrdersService.install(tCkOrders));
    }

    /**
     * 修改出库
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:edit')")
    @Log(title = "出库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCkOrders tCkOrders) {
        return toAjax(tCkOrdersService.updateTCkOrders(tCkOrders));
    }

    /**
     * 删除出库
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:remove')")
    @Log(title = "审核", businessType = BusinessType.UPDATE)
    @DeleteMapping("auditCkOrders/{ids}")
    public AjaxResult auditCkOrders(@PathVariable String[] ids) {
        return toAjax(tCkOrdersService.auditCkOrdersTCkOrdersByIds(ids));
    }

    /**
     * 生成出库任务
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:remove')")
    @Log(title = "生成出库任务", businessType = BusinessType.DELETE)
    @PostMapping("/confirmOut")
    public AjaxResult confirmOut(@RequestBody TCkOrders tCkOrders) {
        tCkOrders.setDeptId(getDeptId());
        return toAjax(tCkOrdersService.confirmOut(tCkOrders));
    }


    /**
     * 任务分配
     */
    /*@PreAuthorize("@ss.hasPermi('system:ckOrders:remove')")*/
    @Log(title = "出库任务分配", businessType = BusinessType.OTHER)
    @PostMapping("/send")
    public AjaxResult send(@RequestBody TCkOrders tCkOrders) {
        tCkOrders.setDeptId(getDeptId());
        return tCkOrdersService.send(tCkOrders);
    }




    /**
     * 删除出库明细
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:remove')")
    @Log(title = "出库", businessType = BusinessType.DELETE)
    @DeleteMapping("/details/{ids}")
    public AjaxResult removeDetails(@PathVariable String[] ids) {
        return toAjax(tCkOrdersService.deleteTCkOrderDetailsByIds(ids));
    }


    /**
     * 删除出库
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:remove')")
    @Log(title = "出库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tCkOrdersService.deleteTCkOrdersByIds(ids));
    }


    /**
     * 启用
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:remove')")
    @Log(title = "出库", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{ids}")
    public AjaxResult disable(@PathVariable String[] ids) {
        return toAjax(tCkOrdersService.disabledTCkOrdersByIds(ids));
    }

    /**
     * 禁用
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:edit')")
    @Log(title = "出库", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{ids}")
    public AjaxResult enable(@PathVariable String[] ids) {
        return toAjax(tCkOrdersService.enableTCkOrdersByIds(ids));
    }

    /**
     * 修改状态
     */
    @PreAuthorize("@ss.hasPermi('system:ckOrders:edit')")
    @Log(title = "出库", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TCkOrders tCkOrders) {
        return toAjax(tCkOrdersService.updateStatus(tCkOrders));
    }

    @GetMapping("/homeCount")
    public AjaxResult homeCount() {
        TCkOrders tCkOrders = new TCkOrders();
        TRkWareNotice notice = new TRkWareNotice();

        Map<String, Integer> map = new HashMap<>();
        map.put("out", tCkOrdersService.homeOutCount(tCkOrders));
        map.put("in", tCkOrdersService.homeInCount(notice));

        return AjaxResult.success(map);
    }
    /**
     * 导入
     */
    @Log(title = "出库数据", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TCkOrderdetail> util = new ExcelUtil<TCkOrderdetail>(TCkOrderdetail.class);
        List<TCkOrderdetail> trList = util.importExcel(file.getInputStream());
        LoginUser loginUser = getLoginUser();
        String operName = loginUser.getUsername();
        String message = tCkOrdersService.importData(trList, updateSupport, operName);
        return AjaxResult.success(message);
    }

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

    // /**
    //  * 发送出库通知单消息
    //  */
    // @Log(title = "发送出库通知单消息", businessType = BusinessType.UPDATE)
    // @PostMapping("/sendOrderNotice")
    // public AjaxResult sendNotice(@RequestBody TCkOrders update) {
    //     update.setCreateBy(getUsername());
    //     update.setDeptId(getDeptId());
    //     return AjaxResult.success(tCkOrdersService.sendOrderNotice(update));
    // }



    /**
     * 发送出库通知到iwms
     */
    @Log(title = "发送出库通知单消息", businessType = BusinessType.UPDATE)
    @PostMapping("/sendiwms")
    public AjaxResult sendIwms(@RequestBody TCkOrders update) {
        update.setCreateBy(getUsername());
        update.setDeptId(getDeptId());
        return AjaxResult.success(tCkOrdersService.sendIwms(update));
    }

    /**
     * 手持出库
     */
    @Log(title = "手持出库", businessType = BusinessType.UPDATE)
    @PostMapping("/issue")
    public AjaxResult handAddGoodsTab(@RequestBody TCkOrders update) {
        return AjaxResult.success(tCkOrdersService.handAddGoodsTab(update));
    }


    /**
     * 出库申请审核后数量统计
     */
    @GetMapping("/statisticCallShelvesNum")
    public AjaxResult statisticCallShelvesNum(TCkOrderdetail tCkOrderdetail) {
        return AjaxResult.success(tCkOrdersService.statisticCallShelvesNum(tCkOrderdetail));
    }
}

