package com.hxhq.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.Study;
import com.hxhq.business.domain.StudyJcgj;
import com.hxhq.business.form.common.SignForm;
import com.hxhq.business.form.systemlog.SystemLogSearchForm;
import com.hxhq.common.core.utils.DateUtils;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.SystemLogMapper;
import com.hxhq.business.domain.SystemLog;
import com.hxhq.business.service.ISystemLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 系统日志Service业务层处理
 *
 * @author hxhq
 * @date 2026-02-02
 */
@Service
public class SystemLogServiceImpl extends ServiceImpl<SystemLogMapper, SystemLog> implements ISystemLogService
{
    /**
     * 查询系统日志列表
     *
     * @param form 系统日志
     * @return 系统日志
     */
    @Override
    public List<SystemLog> queryList(SystemLogSearchForm form)
    {
        QueryWrapper<SystemLog> queryWrapper = Wrappers.query();
        if(StringUtils.isNoneBlank(form.getQmrMc())){
            queryWrapper.like("qmr_mc",form.getQmrMc());
        }
        if(StringUtils.isNoneBlank(form.getJcmc())){
            queryWrapper.and(q->q.eq("jcmc",form.getJcmc())
            .or().eq("jcmc_en",form.getJcmc()));
        }
        if(StringUtils.isNoneBlank(form.getJcnr())){
            queryWrapper.and(q->q.like("jcnr",form.getJcnr())
                    .or().like("jcnr_en",form.getJcnr()));
        }
        if (StringUtils.isNoneBlank(form.getStartDate())) {
            queryWrapper.ge("create_time", form.getStartDate());
        }
        if (StringUtils.isNoneBlank(form.getEndDate())) {
            queryWrapper.lt("create_time", DateUtils.dateTime(DateUtils.addDays(DateUtils.parseDate(form.getEndDate()),1)));
        }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }

    @Override
    public void saveStudyInfo(Study study, String jcnr, String jcnrEn, SignForm signForm) {
        SystemLog info = new SystemLog();
        info.setName(study.getName()+"("+study.getSn()+")");
        info.setNameEn(study.getName()+"("+study.getSn()+")");
        info.setJcnr(jcnr);
        info.setJcnrEn(jcnrEn);
        info.setJcmc(signForm.getQmyy());
        info.setJcmcEn(signForm.getQmyyEn());
        info.setQmrId(signForm.getQmrId()==null? SecurityUtils.getUserId() : signForm.getQmrId());
        info.setQmrMc(signForm.getQmrMc()==null? SecurityUtils.getNickName() : signForm.getQmrMc());
        info.setQmrMcEn(signForm.getQmrMcEn()==null? SecurityUtils.getUsername() : signForm.getQmrMcEn());
        info.setRemark(signForm.getRemark());
        save(info);
    }

    @Override
    public void saveStudyBatch(Study study,List<StudyJcgj> jcgjList) {
        List<SystemLog> logList = new ArrayList<>();
        for(StudyJcgj jcgj : jcgjList){
            SystemLog info = new SystemLog();
            info.setName(study.getName()+"("+study.getSn()+")");
            info.setNameEn(study.getName()+"("+study.getSn()+")");
            info.setJcnr(jcgj.getJcnr());
            info.setJcnrEn(jcgj.getJcnrEn());
            info.setJcmc(jcgj.getJcmc());
            info.setJcmcEn(jcgj.getJcmcEn());
            info.setQmrId(jcgj.getQmrId()==null? SecurityUtils.getUserId() : jcgj.getQmrId());
            info.setQmrMc(jcgj.getQmrMc()==null? SecurityUtils.getNickName() : jcgj.getQmrMc());
            info.setQmrMcEn(jcgj.getQmrMcEn()==null? SecurityUtils.getUsername() : jcgj.getQmrMcEn());
            info.setRemark(jcgj.getRemark());

            logList.add(info);
        }
        if(logList.size()>0){
            saveBatch(logList);
        }
    }

    @Override
    public void saveInfo(String name, String nameEn, String jcmc, String jcmcEn, String jcnr, String jcnrEn, Long qmrid, String qmrMc, String qmrMcEn, String remark) {
        SystemLog info = new SystemLog();
        info.setName(name);
        info.setNameEn(StringUtils.isBlank(nameEn)?name:nameEn);
        info.setJcnr(jcnr);
        info.setJcnrEn(jcnrEn);
        info.setJcmc(jcmc);
        info.setJcmcEn(jcmcEn);
        info.setQmrId(qmrid);
        info.setQmrMc(qmrMc);
        info.setQmrMcEn(qmrMcEn);
        info.setRemark(remark);
        save(info);
    }

    @Override
    public void saveSimpleInfo(String name, String nameEn, String jcmc, String jcmcEn) {
        SystemLog info = new SystemLog();
        info.setName(name);
        info.setNameEn(StringUtils.isBlank(nameEn)?name:nameEn);
        info.setJcmc(jcmc);
        info.setJcmcEn(jcmcEn);

        info.setQmrId(SecurityUtils.getUserId());
        info.setQmrMc(SecurityUtils.getNickName());
        info.setQmrMcEn(SecurityUtils.getUsername());
        save(info);
    }
}
