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.page.TableDataInfo;
import com.bzd.common.enums.BusinessType;
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/cold")
public class ColdDistributionController extends BaseController {
    private String prefix = "dtp/cold";
    @Autowired
    private DTPService dtpService;


    @RequiresPermissions("dtp:cold:view")
    @GetMapping()
    public String cold()
    {
        return prefix + "/cold";
    }

    /**
     *
     * 冷链配送订单查询
     */
    @RequiresPermissions("dtp:cold:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list() throws Exception {
        PageData pd = this.getPageData();

        startPage();

        List<PageData> pageData = dtpService.findColdForList(pd);
        return  getDataTable(pageData);
    }

    /**
     * 冷链配送订单新增页面
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("posts", 1);
        return prefix + "/add";
    }
    /**
     * 冷链配送订单新增保存
     */
    //@RequiresPermissions("server:serv:add")
    @Log(title = "配送订单新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave() throws Exception {
        PageData pd = new PageData();
        pd = this.getPageData();

        Integer integer = dtpService.saveCold(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.softDeleteColdOrder(pd);
        return toAjax(integer);
    }

    @RequiresPermissions("dtp:cold: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.findColdForList(pd).get(0);;
        mmap.putAll(pageData);
        return prefix + "/edit";
    }

    /**
     * 保存訂單修改信息
     */
    @RequiresPermissions("dtp:cold:edit")
    @Log(title = "冷链配送订单管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave() throws Exception
    {
        PageData pd = this.getPageData();
        Integer update = dtpService.editColdOrder(pd);
        if(update!=1){
            return error("修改失败");
        }
        return toAjax(dtpService.editColdOrder(pd));
    }



}
