package com.dgtly.notice.controller;

import java.util.List;

import com.dgtly.common.annotation.DataScope;
import com.dgtly.common.utils.StringUtils;
import com.dgtly.common.utils.file.FileUploadUtils;
import com.dgtly.framework.util.ShiroUtils;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dgtly.common.annotation.Log;
import com.dgtly.common.enums.BusinessType;
import com.dgtly.notice.domain.NoticeSupply;
import com.dgtly.notice.service.INoticeSupplyService;
import com.dgtly.common.core.controller.BaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.utils.poi.ExcelUtil;
import com.dgtly.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【请填写功能名称】Controller
 * 
 * @author dgtly
 * @date 2020-02-13
 */
@Controller
@RequestMapping("/notice/supply")
public class NoticeSupplyController extends BaseController
{
    private String prefix = "notice/supply";

    @Autowired
    private INoticeSupplyService noticeSupplyService;

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

    /**
     * 供应信息列表
     */
    @RequiresPermissions("notice:supply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(NoticeSupply noticeSupply)
    {
        startPage();
        List<NoticeSupply> list = noticeSupplyService.selectNoticeSupplyList(noticeSupply);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("notice:supply:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(NoticeSupply noticeSupply)
    {
        List<NoticeSupply> list = noticeSupplyService.selectNoticeSupplyList(noticeSupply);
        ExcelUtil<NoticeSupply> util = new ExcelUtil<NoticeSupply>(NoticeSupply.class);
        return util.exportExcel(list, "supply");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("notice:supply:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(NoticeSupply noticeSupply)
    {

        if(StringUtils.isNotEmpty(noticeSupply.getPicture())){
            //获取base64文件后缀
            String houzhui = noticeSupply.getPicture().split(";")[0].split("\\/")[1];
            //获取base64值
            String base64 = noticeSupply.getPicture().substring( noticeSupply.getPicture().indexOf(",") + 1);

            //base64转文件 并返回展示url
            String fileUrl = FileUploadUtils.base64ToFile(base64,FileUploadUtils.getDefaultBaseDir()+"/content",System.currentTimeMillis()+"."+houzhui);
            noticeSupply.setPicture(fileUrl);
            noticeSupply.setMerchantId(ShiroUtils.getCompanyId());
            noticeSupply.setCreateBy(ShiroUtils.getLoginName());

        }
        return toAjax(noticeSupplyService.insertNoticeSupply(noticeSupply));
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        NoticeSupply noticeSupply = noticeSupplyService.selectNoticeSupplyById(id);
        mmap.put("noticeSupply", noticeSupply);
        return prefix + "/edit";
    }

    /**
     * 查看供应详情
     */
    @GetMapping("/details/{id}")
    public String details(@PathVariable("id") Long id, ModelMap mmap)
    {
        NoticeSupply noticeSupply = noticeSupplyService.selectNoticeSupplyById(id);
        mmap.put("noticeSupply", noticeSupply);
        return prefix + "/details";
    }

    /**
     * 查看供应详情
     */
    @GetMapping("/editstatus/{id}")
    public String editstatus(@PathVariable("id") Long id, ModelMap mmap)
    {
        NoticeSupply noticeSupply = noticeSupplyService.selectNoticeSupplyById(id);
        mmap.put("noticeSupply", noticeSupply);
        return prefix + "/editstatus";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("notice:supply:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(NoticeSupply noticeSupply)
    {
        if(StringUtils.isNotEmpty(noticeSupply.getPicture())){
            //获取base64文件后缀
            String houzhui = noticeSupply.getPicture().split(";")[0].split("\\/")[1];
            //获取base64值
            String base64 = noticeSupply.getPicture().substring( noticeSupply.getPicture().indexOf(",") + 1);

            //base64转文件 并返回展示url
            String fileUrl = FileUploadUtils.base64ToFile(base64,FileUploadUtils.getDefaultBaseDir()+"/content",System.currentTimeMillis()+"."+houzhui);
            noticeSupply.setPicture(fileUrl);
        }
        return toAjax(noticeSupplyService.updateNoticeSupply(noticeSupply));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("notice:supply:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(noticeSupplyService.deleteNoticeSupplyByIds(ids));
    }
}
