package com.fs.store.controller.store;

import com.fs.framework.service.TokenService;
import com.fs.his.param.FsStoreAuditParam;
import com.fs.store.utils.UserUtil;
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.his.domain.FsStore;
import com.fs.his.domain.StoreLoginUser;
import com.fs.his.service.IFsStoreService;
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")
public class FsStoreController extends BaseController
{
    @Autowired
    private IFsStoreService fsStoreService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询店铺管理列表
     */

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

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

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

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

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

    @Log(title = "店铺管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsStore fsStore)
    {
        return toAjax(fsStoreService.insertFsStore(fsStore));
    }

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

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

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

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

    /**
     * 修改店铺基础信息
     */
    @Log(title = "修改店铺基础信息", businessType = BusinessType.UPDATE)
    @PutMapping("/editData")
    public AjaxResult editData(@RequestBody FsStore fsStore)
    {
        StoreLoginUser loginUser = UserUtil.getLoginUser();
        fsStore.setStoreId(loginUser.getFsStore().getStoreId());
        if (fsStoreService.updateEditData(fsStore) > 0)
        {
            // 更新缓存用户信息
            FsStore 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)
	@DeleteMapping("/{storeIds}")
    public AjaxResult remove(@PathVariable Long[] storeIds)
    {
        return toAjax(fsStoreService.deleteFsStoreByStoreIds(storeIds));
    }

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

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


}
