package com.bzd.web.controller.dtp;

import com.bzd.common.annotation.Log;
import com.bzd.common.config.dao.PageData;
import com.bzd.common.core.controller.BaseController;
import com.bzd.common.core.domain.AjaxResult;
import com.bzd.common.core.domain.entity.DtpDeliveryPrint;
import com.bzd.common.core.page.TableDataInfo;
import com.bzd.common.enums.BusinessType;
import com.bzd.common.utils.poi.ExcelUtil;
import com.bzd.system.service.DTPService;
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.*;
import java.util.List;

/**
 * 配送单据打印
 * creator wsp
 */
@Controller
@RequestMapping("/dtp/print")
public class PrintController extends BaseController {
    private String prefix = "dtp/print";

    @Autowired
    private DTPService dtpService;
    @RequiresPermissions("dtp:print:view")
    @GetMapping()
    public String print()
    {
        return prefix + "/print";
    }

    /**
     * 配送单据打印
     */
    /**
     *
     * 配送单据打印查询
     */
    @RequiresPermissions("dtp:print:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = dtpService.listPrint(pd);
        return  getDataTable(pageData);
    }

    /**
     * 配送单据打印新增页面
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("posts", 1);
        return prefix + "/add";
    }
    /**
     * 配送单据详情页面
     */
    @RequiresPermissions("dtp:print:view")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("id",id);
        PageData pageData = dtpService.listPrint(pd).get(0);
        mmap.putAll(pageData);
        return prefix + "/detail";
    }
    /**
     * 配送单据打印单新增保存
     */
    //@RequiresPermissions("server:serv:add")
    @Log(title = "配送单据打印新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add() throws Exception {
        PageData pd = new PageData();
        pd = this.getPageData();

        Integer integer = dtpService.savePrint(pd);
        return toAjax(integer);
    }

    @Log(title = "配送单据打印删除", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove() throws Exception
    {
        PageData pd = new PageData();
        pd = this.getPageData();
        Integer integer = dtpService.DelPrint(pd);
        return toAjax(integer);
    }

    @RequiresPermissions("dtp:print:edit")
    @GetMapping("/edit/{id}")
    public String view(@PathVariable("id") Long id, ModelMap mmap)throws Exception
    {
        PageData pd = this.getPageData();
        pd.put("id",id);
        PageData pageData = dtpService.listPrint(pd).get(0);
        mmap.putAll(pageData);
        return prefix + "/edit";
    }

    /**
     * 配送单据打印修改信息
     */
    @RequiresPermissions("dtp:print:edit")
    @Log(title = "配送单据打印", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave() throws Exception
    {
        PageData pd = this.getPageData();
        Integer update = dtpService.editPrint(pd);
        if(update!=1){
            return error("修改失败");
        }
        return toAjax(dtpService.editPrint(pd));
    }

    @Log(title = "配送单据信息", businessType = BusinessType.EXPORT)
    @RequiresPermissions("dtp:print:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export()throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<DtpDeliveryPrint> pageData = dtpService.listPrintReport(pd);
        ExcelUtil<DtpDeliveryPrint> util = new ExcelUtil<DtpDeliveryPrint>(DtpDeliveryPrint.class);
        return util.exportExcel(pageData, "配送单据信息数据");

    }

}
