package com.dgtly.goods.controller;

import java.awt.*;
import java.util.List;

import com.dgtly.common.core.text.Convert;
import com.dgtly.common.exception.BusinessException;
import com.dgtly.common.utils.StringUtils;
import com.dgtly.common.utils.file.FileUploadUtils;
import com.dgtly.framework.util.ShiroUtils;
import com.dgtly.system.domain.SysCompany;
import com.dgtly.system.service.ISysCompanyService;
import com.dgtly.system.service.ISysConfigService;
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.goods.domain.GoodsInfo;
import com.dgtly.goods.service.IGoodsInfoService;
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("/goods/info")
public class GoodsInfoController extends BaseController
{
    private String prefix = "goods/info";

    @Autowired
    private IGoodsInfoService goodsInfoService;
    @Autowired
    private ISysCompanyService sysCompanyService;

    @Autowired
    private ISysConfigService configService;

    @RequiresPermissions("goods:info:view")
    @GetMapping()
    public String info(ModelMap mmp)
    {
        Boolean isSystemUser = false;
        /*判断是否是系统内部人员*/
        if(ShiroUtils.getCompanyId()==Long.parseLong(configService.selectConfigByKey("sys.company.id"))){
            isSystemUser=true;
        }
        mmp.addAttribute("isSystemUser",isSystemUser);

        return prefix + "/info";
    }

    /**
     * 查询商品基础详情列表
     */
    @RequiresPermissions("goods:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsInfo goodsInfo)
    {
        /*判断是否是系统内部人员*/
        if(ShiroUtils.getCompanyId()!=Long.parseLong(configService.selectConfigByKey("sys.company.id"))){
            goodsInfo.setCompanyId(ShiroUtils.getCompanyId());
        }else{
            /*系统内部人员只能查看已上架商品*/
            goodsInfo.setPutawayFlag("1");
        }

        startPage();
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        return getDataTable(list);
    }



    /**
     * 新增商品基础详情
     */
    @GetMapping("/add")
    public String add(ModelMap mmp)
    {
        Boolean isSystemUser = false;
        /*判断是否是系统内部人员*/
        if(ShiroUtils.getCompanyId()==Long.parseLong(configService.selectConfigByKey("sys.company.id"))){
            isSystemUser=true;
        }

        SysCompany sysCompany = sysCompanyService.selectSysCompanyById(ShiroUtils.getCompanyId());
        mmp.addAttribute("sysCompany",sysCompany);
        mmp.addAttribute("isSystemUser",isSystemUser);

        return prefix + "/add";
    }

    /**
     * 新增保存商品基础详情
     */
    @RequiresPermissions("goods:info:add")
    @Log(title = "商品基础详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsInfo goodsInfo)
    {

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

            //base64转文件 并返回展示url
            String fileUrl = FileUploadUtils.base64ToFile(base64,FileUploadUtils.getDefaultBaseDir()+"/content",System.currentTimeMillis()+"."+houzhui);
            goodsInfo.setCover(fileUrl);
        }
        return toAjax(goodsInfoService.insertGoodsInfo(goodsInfo));
    }

    /**
     * 修改商品基础详情
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id,String isShow, ModelMap mmap)
    {
        Boolean isSystemUser = false;
        /*判断是否是系统内部人员*/
        if(ShiroUtils.getCompanyId()==Long.parseLong(configService.selectConfigByKey("sys.company.id"))){
            isSystemUser=true;
        }

        SysCompany sysCompany = sysCompanyService.selectSysCompanyById(ShiroUtils.getCompanyId());
        mmap.put("sysCompany",sysCompany);
        mmap.put("isSystemUser",isSystemUser);
        GoodsInfo goodsInfo = goodsInfoService.selectGoodsInfoById(id);
        mmap.put("goodsInfo", goodsInfo);
        mmap.put("isShow", StringUtils.isNotEmpty(isShow)?true:false);
        if(goodsInfo.getSlideshow()!=null){
            mmap.put("slideshows",Convert.toStrArray(goodsInfo.getSlideshow()));
        }
        return prefix + "/edit";
    }

    /**
     * 修改保存商品基础详情
     */
    @RequiresPermissions("goods:info:edit")
    @Log(title = "商品基础详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsInfo goodsInfo)
    {
        if(StringUtils.isNotEmpty(goodsInfo.getCover())){
            //获取base64文件后缀
            String houzhui = goodsInfo.getCover().split(";")[0].split("\\/")[1];
            //获取base64值
            String base64 = goodsInfo.getCover().substring( goodsInfo.getCover().indexOf(",") + 1);

            //base64转文件 并返回展示url
            String fileUrl = FileUploadUtils.base64ToFile(base64,FileUploadUtils.getDefaultBaseDir()+"/content",System.currentTimeMillis()+"."+houzhui);
            goodsInfo.setCover(fileUrl);
        }
        return toAjax(goodsInfoService.updateGoodsInfo(goodsInfo));
    }

    /**
     * 删除商品基础详情
     */
    @RequiresPermissions("goods:info:remove")
    @Log(title = "商品基础详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        List<GoodsInfo> goodsInfos = goodsInfoService.selectGoodsInfoByIds(ids);
        for(GoodsInfo gi:goodsInfos){
            if(gi.getPutawayFlag().equals("1")){
                throw new BusinessException("已上架商品不能删除");
            }
        }
        return toAjax(goodsInfoService.deleteGoodsInfoByIds(ids));
    }

    /**
     * 商品上下架
     */
    @RequiresPermissions("goods:info:putaway")
    @PostMapping("/putaway")
    @ResponseBody
    public Object putaway(String ids,String putawayFlag)
    {
        int i =  goodsInfoService.putawayGooodsInfo(ids,putawayFlag);
        return toAjax(i);
    }
}
