package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.ColumnNavigationBar;
import com.ruoyi.common.core.domain.entity.ColumnNews;
import com.ruoyi.common.core.domain.entity.ImageDatas;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**门户接口,对外开放
 * @author tjf
 * @Date: 2021/12/27/11:36
 */

@RestController
@RequestMapping("/gateway/query")
@CrossOrigin
public class QueryController extends BaseController {

    @Autowired
    private IQueryService queryService;

    @Autowired
    private IColumnNavigationBarService columnNavigationBarService;

    @Autowired
    private IImageDataService imageDataService;

    @Autowired
    private IColumnNewsService columnNewsService;

    @Autowired
    private ISysNoticeService noticeService;

    @Autowired
    private IFriendLinkService friendLinkService;

    @Autowired
    private IInspectInforService inspectInforService;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private IMiniprogramQueryLogService miniprogramQueryLogService;

    /**
     * 门户查询报告信息
     */
    @PostMapping("/getReport")
    public AjaxResult list(ReportQueryLog reportQueryLog)
    {
        return queryService.selectReport(reportQueryLog);
    }


    /**
     * 小程序根据二维码查询
     */
    //@PostMapping("/queryQr")
    public AjaxResult queryQr(ReportQueryLog reportQueryLog)
    {
        return queryService.queryQr(reportQueryLog);
    }

    /**
     * 小程序根据编号查询
     */
    @PostMapping("/queryReportNum")
    public AjaxResult queryReportNum(ReportQueryLog reportQueryLog)
    {
        return queryService.queryReportNum(reportQueryLog);
    }


    /**
     * 获取栏目导航下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(ColumnNavigationBar columnNavigationBar)
    {
        //默认查询是导航
        columnNavigationBar.setIsNavigationBar("Y");
        List<ColumnNavigationBar> columnNavigationBars = columnNavigationBarService.selectColumnNavigationBarList(columnNavigationBar);
        return AjaxResult.success(columnNavigationBarService.buildColumnTreeSelect(columnNavigationBars));
    }

    /**
     * 获取各个部分首页标题
     */
    @GetMapping("/titleList")
    public AjaxResult titleList(ColumnNavigationBar columnNavigationBar)
    {
        List<ColumnNavigationBar> columnNavigationBars = columnNavigationBarService.selectColumnNavigationBarListMenHu(columnNavigationBar);
        return AjaxResult.success(columnNavigationBars);
    }

    /**
     * 获取首大图
     */
    @PostMapping("/getRotationChart")
    public AjaxResult getRotationChart(ImageDatas imageDatas)
    {
        List<ImageDatas> list = imageDataService.selectImageDatasListMenHu(imageDatas);
        return AjaxResult.success(list);
    }

    /**
     * 获取首页轮播图
     */
    @PostMapping("/getRotation")
    public AjaxResult getRotation()
    {
        List<ColumnNews> list = columnNewsService.getRotation();
        return AjaxResult.success(list);
    }


    /**
     * 提交送检信息
     */
    @PostMapping("/sendInspectInFor")
    public AjaxResult sendInspectInFor(InspectInfor inspectInfor)
    {
/*        if (UserConstants.NOT_UNIQUE.equals(inspectInforService.checkSampleNameUnique(inspectInfor)))
        {
            return AjaxResult.error("新增样品名称'" + inspectInfor.getSampleName() + "'失败，样品名称已存在");
        }*/
        return toAjax(inspectInforService.insertInspectInfor(inspectInfor));
    }

    /**
     * 获取新闻文章
     */
    @PostMapping("/getNewS")
    public TableDataInfo getNewS(ColumnNews columnNews)
    {
        startPage();
        List<ColumnNews> list = columnNewsService.selectColumnNewsListMenHu(columnNews);
        return getDataTable(list);
    }

    /**
     * 获取新闻文章详情
     */
    @GetMapping("/{newsId}")
    public AjaxResult getNewSDetail(@PathVariable("newsId") Integer newsId)
    {
        ColumnNews columnNews = columnNewsService.selectColumnNewsDetailMenHu(newsId);
        return AjaxResult.success(columnNews);
    }

    /**
     * 获取通知公告
     */
    @PostMapping("/getNotice")
    public TableDataInfo getNotice(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeListMenHu(notice);
        return getDataTable(list);
    }

    /**
     * 获取友链
     */
    @PostMapping("/getFriendLink")
    public AjaxResult getFriendLink()
    {
        List<FriendLink> list = friendLinkService.selectFriendLinkList(new FriendLink());
        return AjaxResult.success(list);
    }


    /**
     * 给档案拥有者发送询问短信  todo做好防御
     * @param reportQueryLog
     * @return
     */
    @PostMapping("/sendQueryNum")
    @RepeatSubmit
    public AjaxResult sendQueryNum(ReportQueryLog reportQueryLog)
    {
        return  queryService.sendQueryNum(reportQueryLog);
    }

    /**
     * 获取验证码 todo做好防御
     * @param reportQueryLog
     * @return
     */
    @PostMapping("/getCode")
    @RepeatSubmit
    public AjaxResult getCode( ReportQueryLog reportQueryLog)
    {
        return queryService.sendSms(reportQueryLog);
    }

    /**
     * 门户二维码上传请求
     */
    @PostMapping("/common/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadQrPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String originalFilename = file.getOriginalFilename();
            String url = RuoYiConfig.getStaticIp() + fileName;
            String profile = RuoYiConfig.getUploadQrPath();
            String absolutePath = fileName.replace("/profile/qrUpload",profile);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("absolutePath", absolutePath);
            ajax.put("originalFilename", originalFilename);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.success(data);
    }


    /**
     * 查询小程序查询历史记录列表
     */
    @PostMapping("/miniProgramQueryLog")
    public TableDataInfo list(MiniprogramQueryLog miniprogramQueryLog)
    {
        startPage();
        List<MiniprogramQueryLog> list = miniprogramQueryLogService.selectMiniprogramQueryLogList(miniprogramQueryLog);
        return getDataTable(list);
    }
}
