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.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.exception.base.BaseException;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.utils.txocr.TxOcrClient;
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.param.FsStoreScrmInfoParam;
import com.fs.hisStore.service.IFsStoreScrmService;
import com.fs.hisStore.utils.StoreAuditLogUtil;
import com.fs.hisStore.utils.UserUtil;
import com.fs.hisStore.vo.FsStoreDetailsScrmVo;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
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;

    @Autowired
    private StoreAuditLogUtil storeAuditLogUtil;

    /**
     * 新增或修改时候校验上传的营业执照是否包含药品零售
     */
    @GetMapping("/businessLicenseCheck")
    public R businessLicenseCheck(String imageUrl){
        return R.ok().put("data", TxOcrClient.isContains(imageUrl, null));
    }

    /**
     * 查询店铺管理列表
     */
    @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 FsStoreScrmInfoParam fsStore) {
        return toAjax(fsStoreService.updateStoreInfo(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 R remove(@PathVariable Long[] storeIds) {
        return fsStoreService.deleteFsStoreByStoreIds(storeIds);
    }

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

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

    /**
     * 获取店铺管理详细信息
     */
    @GetMapping("/getStoreInfo")
    public AjaxResult getStoreInfo() {
        FsStoreScrm user = UserUtil.getLoginUser().getFsStore();
        FsStoreDetailsScrmVo fsStore = fsStoreService.getStoreInfoByStoreId(user.getStoreId());
        return AjaxResult.success(fsStore);
    }

    /**
     * 商家店铺资质提示信息
     */
    @Log(title = "商家端：商家证件即将过期或已过期提醒", businessType = BusinessType.OTHER)
    @GetMapping("/queryValidStoreLastInfo")
    public R queryValidStoreLastInfo() {
        StoreLoginUserScrm loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (loginUser != null) {
            if (loginUser.getFsStore() != null) {
                // 判断account是否为空
                String account = loginUser.getFsStore().getAccount();
                if (account == null && account.isEmpty()) {
                    throw new BaseException("请先完善店铺账户信息");
                }
                List<String> list = fsStoreService.queryValidStoreLastInfo(loginUser.getFsStore().getStoreId());
                if (!Collections.isEmpty(list)) {
                    return R.ok().put("code", 200).put("data", list);
                }
            }
        }
        return R.ok().put("code", 0);
    }


    /**
     * 商品审核日志
     * */
    @GetMapping("/auditLog")
    public R auditLog(){
        StoreLoginUserScrm loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return R.ok().put("auditLog",storeAuditLogUtil.selectOperLogByMainId(loginUser.getFsStore().getStoreId(),"店铺"));
    }

    /**
     * 店铺资质提醒接口
     * **/
    @Log(title = "商家端：商家满6个月证件更换提醒", businessType = BusinessType.OTHER)
    @GetMapping("/qualificationReminder")
    public R qualificationReminder(){
        StoreLoginUserScrm loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return fsStoreService.qualificationReminder(loginUser.getFsStore().getStoreId());
    }
}
