package com.hxhq.business.controller;

import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hxhq.business.form.systemlog.SystemLogSearchForm;
import com.hxhq.common.core.utils.DateUtils;
import com.hxhq.common.core.utils.ServletUtils;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.core.utils.poi.ExcelUtil;
import com.hxhq.common.core.web.domain.AjaxResult;
import com.hxhq.common.security.annotation.RequiresPermissions;
import com.hxhq.system.api.domain.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hxhq.business.domain.SystemLog;
import com.hxhq.business.service.ISystemLogService;
import com.hxhq.common.core.web.controller.BaseController;
import com.hxhq.common.core.web.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;


/**
 * 系统日志Controller
 *
 * @author hxhq
 * @date 2026-02-02
 */
@RestController
@RequestMapping("/business/systemLog")
public class SystemLogController extends BaseController
{
    @Autowired
    private ISystemLogService systemLogService;

    /**
     * 查询系统日志列表
     */
    @GetMapping("/list")
    @RequiresPermissions("business:systemLog:operate")
    public TableDataInfo list(SystemLogSearchForm form)
    {
        startPage();
        List<SystemLog> list = systemLogService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 查询系统日志列表
     */
    @GetMapping("/czlxList")
    @RequiresPermissions("business:systemLog:operate")
    public AjaxResult czlxList()
    {
        QueryWrapper<SystemLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct jcmc,jcmc_en");
        List<SystemLog> list = systemLogService.list(queryWrapper);
        return AjaxResult.success(list);
    }

    /**
     * 查询系统日志列表
     */
    @PostMapping("/export")
    @RequiresPermissions("business:systemLog:operate:export")
    public void export(HttpServletResponse response, SystemLogSearchForm form)
    {
        List<SystemLog> list = systemLogService.queryList(form);
        for(SystemLog l : list){
            l.setCzsj(DateUtils.parseDateToStr(l.getCreateTime()==null?"": DateUtils.YYYY_MM_DD_HH_MM_SS,l.getCreateTime()));
            l.setCzlx("zh_CN".equals(ServletUtils.getRequest().getHeader("lang"))?l.getJcmc():l.getJcmcEn());
            String czxq = "";
            if("zh_CN".equals(ServletUtils.getRequest().getHeader("lang"))){
                try{
                    JSONArray jsonArray = JSONArray.parseArray(l.getJcnr());
                    for(int i=0;i<jsonArray.size();i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        czxq+=jsonObject.getString("name")+"："+jsonObject.getString("value")+(i==jsonArray.size()-1?"":"；");
                    }
                    czxq = (StringUtils.isNoneBlank(l.getName())?("【"+l.getName()+"】"):"")+czxq+(StringUtils.isNoneBlank(l.getRemark())?("；备注:"+l.getRemark()):"");
                }catch (Exception e){
                    czxq = (StringUtils.isNoneBlank(l.getName())?("【"+l.getName()+"】"):"")+(StringUtils.isNoneBlank(l.getJcnr())?l.getJcnr():"")+(StringUtils.isNoneBlank(l.getRemark())?("；备注:"+l.getRemark()):"");
                }
            }else{
                try{
                    JSONArray jsonArray = JSONArray.parseArray(l.getJcnrEn());
                    for(int i=0;i<jsonArray.size();i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        czxq+=jsonObject.getString("name")+"："+jsonObject.getString("value")+(i==jsonArray.size()-1?"":"；");
                    }
                    czxq = (StringUtils.isNoneBlank(l.getNameEn())?("【"+l.getNameEn()+"】"):"")+czxq+(StringUtils.isNoneBlank(l.getRemark())?("；Comment:"+l.getRemark()):"");
                }catch (Exception e){
                    czxq = (StringUtils.isNoneBlank(l.getNameEn())?("【"+l.getNameEn()+"】"):"")+(StringUtils.isNoneBlank(l.getJcnrEn())?l.getJcnrEn():"")+(StringUtils.isNoneBlank(l.getRemark())?("；Comment:"+l.getRemark()):"");
                }
            }
            l.setCzxq(czxq);
        }

        ExcelUtil<SystemLog> util = new ExcelUtil<SystemLog>(SystemLog.class);
        util.exportExcel(response, list, "操作日志");
    }
}
