package com.ruoyi.web.controller.info;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.info.hospital.domain.bo.HospitalBo;
import com.ruoyi.info.hospital.domain.bo.HospitalQrCodeBo;
import com.ruoyi.info.hospital.domain.bo.HospitalRackUpdateBo;
import com.ruoyi.info.hospital.domain.vo.HospitalSimpleInfoVo;
import com.ruoyi.info.hospital.domain.vo.HospitalVo;
import com.ruoyi.info.hospital.service.IHospitalService;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.weixin.service.WxMinService;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;

/**
 * 医院信息Controller
 *
 * @author baifc
 * @date 2025-09-20
 */
@Validated
@Api(value = "医院信息控制器", tags = {"医院信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/info/hospital")
public class HospitalController extends BaseController {

    private final IHospitalService iHospitalService;

    private final WxMinService wxMinService;

    private final ISysOssService iSysOssService;
    @Value("${oss.filePathOssDomain}")
    private String filePathOssDomain;

    /**
     * 分页查询医院信息列表
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("查询医院信息分页")
    @SaCheckPermission("info:hospital:list")
    @GetMapping("/page")
    public TableDataInfo<HospitalVo> page(@Validated(QueryGroup.class) HospitalBo bo, PageQuery pageQuery) {
        return iHospitalService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取医院信息详细信息
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("获取医院信息详细信息")
    @SaCheckPermission("info:hospital:query")
    @GetMapping("/info/{id}")
    public R<HospitalVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(iHospitalService.queryById(id));
    }

    /**
     * 新增医院信息
     */
    @FilePathSplicing(type = FilePathSplicingType.REQUEST)
    @ApiOperation("新增医院信息")
    @SaCheckPermission("info:hospital:add")
    @Log(title = "医院信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HospitalBo bo) {
        return toAjax(iHospitalService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改医院信息
     */
    @FilePathSplicing(type = FilePathSplicingType.REQUEST)
    @ApiOperation("修改医院信息")
    @SaCheckPermission("info:hospital:edit")
    @Log(title = "医院信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HospitalBo bo) {
        return toAjax(iHospitalService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除医院信息
     */
    @ApiOperation("删除医院信息")
    @SaCheckPermission("info:hospital:remove")
    @Log(title = "医院信息", businessType = BusinessType.DELETE)
    @PostMapping("/del/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iHospitalService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 修改医院上下架
     *
     */
    @ApiOperation("修改医院上下架状态")
    @SaCheckPermission("info:hospital:edit")
    @Log(title = "修改医院上下架状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateRackStatus")
    public R<Void> updateRackStatus(@Validated @RequestBody HospitalRackUpdateBo bo) {
        //  修改医院上下架
        iHospitalService.updateRackStatus(bo);
        return R.ok();
    }

    /**
     * 获取所有医院列表
     */
    @ApiOperation("查询医院信息列表")
    @GetMapping("/simpleList")
    public R<List<HospitalSimpleInfoVo>> simpleList() {
        List<HospitalVo> hospitalVoList = iHospitalService.queryList(new HospitalBo());
        return R.ok(BeanCopyUtils.copyList(hospitalVoList, HospitalSimpleInfoVo.class));
    }


    @ApiOperation("获取二维码")
    @PostMapping("/getQrCode")
    public R<Map<String,String>> getQrCode(@RequestBody HospitalQrCodeBo bo) {

        HospitalVo hospitalVo = iHospitalService.queryById(bo.getHospitalId());
        if(StringUtils.isNotBlank(hospitalVo.getQrCodeUrl())) {
            Map<String,String> map = new HashMap<>();
            map.put("url", hospitalVo.getQrCodeUrl());
            return R.ok(map);
        }

        String t = getSceneType(bo);
        String scene = String.format("t=%s&id=%s", t, bo.getHospitalId());
        byte[] bytes = wxMinService.getWxMinCode(bo.getPath(), scene);
        String filename = IdUtil.fastSimpleUUID() + ".jpg";
        SysOss sysOss = iSysOssService.localUpload(bytes, filename);

        iHospitalService.updateQrCodeUrl(bo.getHospitalId(), sysOss.getUrl());
        Map<String,String> map = new HashMap<>();
        map.put("url",filePathOssDomain + sysOss.getUrl());
        return R.ok(map);
    }

    private String getSceneType(HospitalQrCodeBo bo) {
        switch (bo.getSceneType()){
            case SHOP_INDEX:
                return "i";
            default:
                return "";
        }
    }




}
