package com.fs.hisStore.controller.store;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.framework.service.TokenServiceScrm;
import com.fs.his.param.FsStoreAuditParam;
import com.fs.hisStore.domain.FsStoreScrm;
import com.fs.hisStore.domain.StoreLoginUserScrm;
import com.fs.hisStore.service.IFsStoreScrmService;
import com.fs.hisStore.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺管理Controller
 *
 * @author fs
 * @date 2023-06-15
 */
@RestController
@RequestMapping("/store/store/store")
public class FsStoreScrmController extends BaseController
{
    @Autowired
    private IFsStoreScrmService fsStoreService;
    @Autowired
    private TokenServiceScrm tokenService;
    /**
     * 查询店铺管理列表
     */

    @GetMapping("/list")
    public TableDataInfo list(FsStoreScrm fsStore)
    {
        startPage();
        List<FsStoreScrm> list = fsStoreService.selectFsStoreList(fsStore);
        return getDataTable(list);
    }

    /**
     * 导出店铺管理列表
     */

    @Log(title = "店铺管理", businessType = BusinessType.EXPORT,logParam = {"店铺","导出店铺信息"},isStoreLog = true)
    @GetMapping("/export")
    public AjaxResult export(FsStoreScrm fsStore)
    {
        List<FsStoreScrm> list = fsStoreService.selectFsStoreList(fsStore);
        ExcelUtil<FsStoreScrm> util = new ExcelUtil<FsStoreScrm>(FsStoreScrm.class);
        return util.exportExcel(list, "店铺管理数据");
    }

    /**
     * 获取店铺管理详细信息
     */
    @GetMapping(value = "/{storeId}")
    public AjaxResult getInfo(@PathVariable("storeId") Long storeId)
    {
        return AjaxResult.success(fsStoreService.selectFsStoreByStoreId(storeId));
    }

    /**
     * 新增店铺管理
     */

    @Log(title = "店铺管理", businessType = BusinessType.INSERT,logParam = {"店铺","新增店铺信息"},isStoreLog = true)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreScrm fsStore)
    {
        return toAjax(fsStoreService.insertFsStore(fsStore).intValue());
    }

    /**
     * 修改店铺管理
     */

    @Log(title = "店铺管理", businessType = BusinessType.UPDATE,logParam = {"店铺","修改店铺信息"},isStoreLog = true)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreScrm fsStore)
    {
        return toAjax(fsStoreService.updateFsStore(fsStore));
    }
    /**
     * 修改店铺logo
     */
    @Log(title = "店铺管理", businessType = BusinessType.UPDATE,logParam = {"店铺","修改店铺LOGO信息"},isStoreLog = true)
    @PutMapping("/editLogo")
    public AjaxResult editLogo(@RequestBody FsStoreScrm fsStore)
    {
        StoreLoginUserScrm loginUser = UserUtil.getLoginUser();
        fsStore.setStoreId(loginUser.getFsStore().getStoreId());

        if (fsStoreService.updateFsStoreLogo(fsStore) > 0)
        {
            // 更新缓存用户信息
            FsStoreScrm fsStore1 = loginUser.getFsStore();
            fsStore1.setLogoUrl(fsStore.getLogoUrl());
            loginUser.setFsStore(fsStore1);
            tokenService.setLoginUser(loginUser);
            return AjaxResult.success();
        }

        return AjaxResult.error("修改个人信息异常，请联系管理员");
    }

    /**
     * 修改店铺基础信息
     */
    @Log(title = "修改店铺基础信息", businessType = BusinessType.UPDATE,logParam = {"店铺","修改店铺基础信息"},isStoreLog = true)
    @PutMapping("/editData")
    public AjaxResult editData(@RequestBody FsStoreScrm fsStore)
    {
        StoreLoginUserScrm loginUser = UserUtil.getLoginUser();
        fsStore.setStoreId(loginUser.getFsStore().getStoreId());
        if (fsStoreService.updateEditData(fsStore) > 0)
        {
            // 更新缓存用户信息
            FsStoreScrm fsStore1 = loginUser.getFsStore();
            fsStore1.setPhone(fsStore.getPhone());
            fsStore1.setAddress(fsStore.getAddress());
            fsStore1.setStoreName(fsStore.getStoreName());
            fsStore1.setAccount(fsStore.getAccount());
            fsStore1.setLogoUrl(fsStore.getLogoUrl());
            loginUser.setFsStore(fsStore1);
            tokenService.setLoginUser(loginUser);
            return AjaxResult.success();
        }

        return AjaxResult.error("修改个人信息异常，请联系管理员");
    }

    /**
     * 删除店铺管理
     */

    @Log(title = "店铺管理", businessType = BusinessType.DELETE,logParam = {"店铺","删除店铺信息"},isStoreLog = true)
	@DeleteMapping("/{storeIds}")
    public AjaxResult remove(@PathVariable Long[] storeIds)
    {
        return toAjax(fsStoreService.deleteFsStoreByStoreIds(storeIds));
    }

    /**
     * 修改店铺管理
     */

    @Log(title = "店铺管理", businessType = BusinessType.UPDATE,logParam = {"店铺","店铺审核"},isStoreLog = true)
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody FsStoreAuditParam fsStore)
    {
        return toAjax(fsStoreService.updateFsStoreAudit(fsStore));
    }


}
