package com.fs.hisStore.controller;

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.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.his.param.FsStoreAuditParam;
import com.fs.hisStore.domain.FsStoreScrm;
import com.fs.hisStore.service.IFsStoreScrmService;
import com.fs.hisStore.utils.StoreAuditLogUtil;
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/his/store")
public class FsStoreScrmController extends BaseController
{
    @Autowired
    private IFsStoreScrmService fsStoreService;

    @Autowired
    private StoreAuditLogUtil storeAuditLogUtil;

    /**
     * 查询店铺管理列表
     */
    @PreAuthorize("@ss.hasPermi('store:his:store:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsStoreScrm fsStore)
    {
        startPage();
        List<FsStoreScrm> list = fsStoreService.selectFsStoreList(fsStore);
        for (FsStoreScrm store : list) {
            store.setPhone(ParseUtils.parsePhone(store.getPhone()));
        }
        return getDataTable(list);
    }

    /**
     * 导出店铺管理列表
     */
    @PreAuthorize("@ss.hasPermi('store:his:store:export')")
    @Log(title = "店铺管理", businessType = BusinessType.EXPORT,logParam = {"店铺","导出店铺信息"},isStoreLog = true)
    @GetMapping("/export")
    public AjaxResult export(FsStoreScrm fsStore)
    {
        storeAuditLogUtil.generateOperId();
        List<FsStoreScrm> list = fsStoreService.selectFsStoreList(fsStore);
        for (FsStoreScrm store : list) {
            store.setPhone(ParseUtils.parsePhone(store.getPhone()));
        }
        ExcelUtil<FsStoreScrm> util = new ExcelUtil<FsStoreScrm>(FsStoreScrm.class);
        return util.exportExcel(list, "店铺管理数据");
    }

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

    /**
     * 新增店铺管理
     */
    @PreAuthorize("@ss.hasPermi('store:his:store:add')")
    @Log(title = "店铺管理", businessType = BusinessType.INSERT,logParam = {"店铺","新增店铺信息"},isStoreLog = true)
    @PostMapping
    public AjaxResult add(@RequestBody FsStoreScrm fsStore)
    {
        storeAuditLogUtil.addOperLog(fsStoreService.insertFsStore(fsStore));
        return AjaxResult.success();
    }

    /**
     * 修改店铺管理
     */
    @PreAuthorize("@ss.hasPermi('store:his:store:edit')")
    @Log(title = "店铺管理", businessType = BusinessType.UPDATE,logParam = {"店铺","修改店铺信息"},isStoreLog = true)
    @PutMapping
    public AjaxResult edit(@RequestBody FsStoreScrm fsStore)
    {

        if (fsStore.getPhone()!=null&&fsStore.getPhone().contains("*")){
            fsStore.setPhone(null);
        }
        return toAjax(fsStoreService.updateFsStore(fsStore));
    }

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

    /**
     * 店铺审核
     */
    @PreAuthorize("@ss.hasPermi('store:his:store:audit')")
    @Log(title = "店铺审核", businessType = BusinessType.AUDIT,logParam = {"店铺","店铺审核"},isStoreLog = true
    ,logParamExpression = "#p0.getIsAudit()==1?new String[]{'店铺','店铺审核通过'}: new String[]{'店铺','店铺审核退回'}")
    @PutMapping("/audit")
    public AjaxResult audit(@RequestBody FsStoreAuditParam fsStore)
    {
        return toAjax(fsStoreService.updateFsStoreAudit(fsStore));
    }

    /**
     * 重置店铺密码
     * */
    @PreAuthorize("@ss.hasPermi('store:his:store:refresh')")
    @Log(title = "店铺管理", businessType = BusinessType.UPDATE,logParam = {"店铺","重置店铺密码"},isStoreLog = true)
    @PutMapping("/refresh/{storeId}")
    public AjaxResult refresh(@PathVariable Long storeId)
    {
        return toAjax(fsStoreService.refreshFsStore(storeId));
    }


    /**
     * 店铺审核日志
     * */
    @PreAuthorize("@ss.hasPermi('store:his:store:auditLog')")
    @GetMapping("/auditLog/{storeId}")
    public R auditLog(@PathVariable Long storeId){
        return R.ok().put("auditLog",storeAuditLogUtil.selectOperLogByMainId(storeId));
    }
}
