package com.bzd.web.controller.spgl;

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.common.utils.StringUtils;
import com.bzd.system.service.TypeDataService;
import com.bzd.system.service.spgl.SPProductinfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

import static com.bzd.common.config.dao.ConfigFile.result;

/**
 *
 * 门店积分商品列表 前端控制器Controller
 * @author LiXiangFei
 * @since 2024-12-17
 */
@Controller
@RequestMapping(value = "sp/sp")
public class SPConfigInfoController extends BaseController {

    // 页面跳转前缀
    private String prefix = "spgl";

    @Autowired
    private SPProductinfoService sPProductinfoService;
    @Autowired
    private TypeDataService typeDataService;

    @GetMapping("/typeDate")
    @ResponseBody
    public AjaxResult typeDate() throws Exception
    {
        AjaxResult ajax = new AjaxResult();
        PageData pd = this.getPageData();
        List<PageData> list = typeDataService.typeDate(pd);
        ajax.put("code", 200);
        ajax.put("value", list);

        return ajax;
    }

    @GetMapping("/typeDate2")
    @ResponseBody
    public AjaxResult typeDate2(@RequestParam(value = "jbtype", required = false) String jbtype, ModelMap mmap) throws Exception {
        AjaxResult ajax = new AjaxResult();
        PageData pd = this.getPageData();
        List<PageData> list = typeDataService.typeDate2(pd);
        ajax.put("code", 200);
        ajax.put("value", list);
        return ajax;
    }


    @RequiresPermissions("sp:sp:view")
    @GetMapping("/SPProductinfoListView")
    public String SPProductinfoListView() {
        return prefix + "/SPProductinfoList";
    }

    @RequiresPermissions("sp:sp:list")
    @PostMapping("/SPProductinfoList")
    @ResponseBody
    public TableDataInfo SPProductinfoList() throws Exception {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> pageData = sPProductinfoService.findSPProductinfoList(pd);
        return getDataTable(pageData);
    }

    @RequiresPermissions("sp:sp:add")
    @GetMapping("/SPProductAdd")
    public String SPProductAdd() throws Exception {
        return prefix + "/SPProductAdd";
    }

    @RequiresPermissions("sp:sp:add")
    @GetMapping("/SPProductEdit/{id}")
    public String SPProductEdit(@PathVariable("id") Long id, ModelMap mmap) throws Exception {
        PageData pd = this.getPageData();
        pd.put("id", id);
        PageData pageData = sPProductinfoService.findAllList(pd);
        mmap.putAll(pageData);;
        return prefix + "/SPProductEdit";
    }

    @RequiresPermissions("dtp:sp:add")
    @PostMapping("/ProductAdd")
    @ResponseBody
    public AjaxResult ProductAdd() {
        try {
            PageData pd = this.getPageData();
         String productCode = (String) pd.get("product_code");
            if (StringUtils.isEmpty(productCode)){
                return AjaxResult.error("商品编号不能为空");
            }
            PageData pd2=new PageData();
            pd2.put("product_code",productCode);
            // 直接service 保存
            List<PageData> pageData = sPProductinfoService.findSPProductinfoListBYProductCode(pd2);
            if (pageData.size() > 0){
                return AjaxResult.error("商品编号已存在");
            }
            Integer result = sPProductinfoService.productAdd(pd);
            if (result == 1) {
                return AjaxResult.success("新增成功");
            } else {
                logger.error("Failed to update 表ProductAdd with ID: {}", pd.get("id"));
                return AjaxResult.error("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("添加失败 请联系管理员");
        }
    }

    @RequiresPermissions("dtp:sp:edit")
    @PostMapping("/ProductUpdate")
    @ResponseBody
    public AjaxResult ProductUpdate() {
        try {
            PageData pd = this.getPageData();
            Integer result = sPProductinfoService.ProductUpdate(pd);
            if (result == 1) {
                return AjaxResult.success("修改成功");
            } else {
                logger.error("Failed to update 表ProductUpdate with ID: {}", pd.get("id"));
                return AjaxResult.error("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败 请联系管理员");
        }
    }
    @RequiresPermissions("dtp:sp:add")
    @PostMapping("/ProductAdd2")
    @ResponseBody
    public AjaxResult ProductAdd2() {
        try {
            PageData pd = this.getPageData();
            // 直接service 保存
            Integer result = sPProductinfoService.productAdd2(pd);
            if (result == 1) {
                return AjaxResult.success("新增成功");
            } else {
                logger.error("Failed to update 表ProductAdd2 with ID: {}", pd.get("id"));
                return AjaxResult.error("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    @RequiresPermissions("dtp:sp:add")
    @PostMapping("/ProductAdd3")
    @ResponseBody
    public AjaxResult ProductAdd3() {
        try {
            PageData pd = this.getPageData();
            // 直接service 保存
            Integer result = sPProductinfoService.productAdd3(pd);
            if (result == 1) {
                return AjaxResult.success("新增成功");
            } else {
                logger.error("Failed to update 表ProductAdd3 with ID: {}", pd.get("id"));
                return AjaxResult.error("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    @RequiresPermissions("dtp:sp:add")
    @PostMapping("/ProductAdd4")
    @ResponseBody
    public AjaxResult ProductAdd4() {
        try {
            PageData pd = this.getPageData();
            // 直接service 保存
            Integer result = sPProductinfoService.productAdd4(pd);
            if (result == 1) {
                return AjaxResult.success("新增成功");
            } else {
                logger.error("Failed to update 表ProductAdd4 with ID: {}", pd.get("id"));
                return AjaxResult.error("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    @RequiresPermissions("dtp:sp:add")
    @PostMapping("/ProductAdd5")
    @ResponseBody
    public AjaxResult ProductAdd5() {
        try {
            PageData pd = this.getPageData();
            // 直接service 保存
            Integer result = sPProductinfoService.productAdd5(pd);
            if (result == 1) {
                return AjaxResult.success("新增成功");
            } else {
                logger.error("Failed to update 表ProductAdd5 with ID: {}", pd.get("id"));
                return AjaxResult.error("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    @RequiresPermissions("dtp:sp:add")
    @PostMapping("/ProductAdd6")
    @ResponseBody
    public AjaxResult ProductAdd6() {
        try {
            PageData pd = this.getPageData();
            // 直接service 保存
            Integer result = sPProductinfoService.productAdd6(pd);
            if (result == 1) {
                return AjaxResult.success("新增成功");
            } else {
                logger.error("Failed to update 表ProductAdd6 with ID: {}", pd.get("id"));
                return AjaxResult.error("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @RequiresPermissions("dtp:sp:add")
    @PostMapping("/ProductAdd12")
    @ResponseBody
    public AjaxResult ProductAdd12() {
        try {
            PageData pd = this.getPageData();
            // 直接service 保存
            Integer result = sPProductinfoService.productAdd12(pd);
            if (result == 1) {
                return AjaxResult.success("新增成功");
            } else {
                logger.error("Failed to update 表ProductAdd12 with ID: {}", pd.get("id"));
                return AjaxResult.error("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @RequiresPermissions("sp:sp:remove")
    @Log(title = "新增配置删除", businessType = BusinessType.DELETE)
    @PostMapping("/SPProductRemove")
    @ResponseBody
    public AjaxResult SPProductRemove() throws Exception {
        PageData pd = this.getPageData();
        Integer integer = sPProductinfoService.SPProductinfoRemove(pd);
        return toAjax(integer);
    }

}
