package com.hxhq.business.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.MjyJcgj;
import com.hxhq.business.domain.YqJcgj;
import com.hxhq.business.enums.zykgl.JcgjlxEnum;
import com.hxhq.business.enums.zykgl.JcmcysEnum;
import com.hxhq.business.form.yq.YqForm;
import com.hxhq.business.form.yq.YqSearchForm;
import com.hxhq.business.service.IYqJcgjService;
import com.hxhq.business.utils.ObjectCompareUtil;
import com.hxhq.common.core.domain.MpBaseEntity;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.common.security.utils.SecurityUtils;
import com.hxhq.system.api.domain.SysUser;
import com.hxhq.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.YqMapper;
import com.hxhq.business.domain.Yq;
import com.hxhq.business.service.IYqService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 仪器管理Service业务层处理
 *
 * @author HanLong
 * @date 2025-12-20
 */
@Service
public class YqServiceImpl extends ServiceImpl<YqMapper, Yq> implements IYqService
{
    @Autowired
    private IYqJcgjService yqJcgjService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询仪器管理列表
     * @return 仪器管理
     */
    @Override
    public List<Yq> queryList(YqSearchForm form)
    {
        QueryWrapper<Yq> queryWrapper = Wrappers.query();
        if(StringUtils.isNotEmpty(form.getMc())) {
            queryWrapper.like("y.`mc`", form.getBh());
        }
        if(StringUtils.isNotEmpty(form.getBh())) {
            queryWrapper.like("y.bh", form.getBh());
        }
        if(StringUtils.isNotEmpty(form.getLy())) {
            queryWrapper.like("y.ly", form.getLy());
        }
        if(StringUtils.isNotEmpty(form.getStartDate())) {
            queryWrapper.ge("y.jzrq", form.getStartDate());
        }
        if(StringUtils.isNotEmpty(form.getEndDate())) {
            queryWrapper.le("y.jzrq", form.getEndDate());
        }
        if(StringUtils.isNotEmpty(form.getBmMc())) {
            queryWrapper.like("d.dept_name", form.getBmMc());
        }
        queryWrapper.orderByDesc("y.id");
        return baseMapper.queryList(queryWrapper);
    }

    @Override
    public void addYq(YqForm yqForm) {
        SysUser qmr = sysUserService.selectUserById(yqForm.getQmrId());
        sysUserService.checkPassword(qmr, yqForm.getQmrmm(), false);
        Yq yq = new Yq();
        BeanUtils.copyProperties(yqForm, yq);
        save(yq);
        yqJcgjService.saveJcgj(yq.getId(), JcgjlxEnum.bj.getValue(), "新增仪器", "Add Instrument", JcmcysEnum.blue.getValue(), null, null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateYq(YqForm yqForm) {
        SysUser qmr = sysUserService.selectUserById(yqForm.getQmrId());
        sysUserService.checkPassword(qmr, yqForm.getQmrmm(), false);

        Yq yq = getById(yqForm.getId());
        if(yq == null) {
            throw new ServiceException("仪器不存在或已删除");
        }

        List<ObjectCompareUtil.FieldChange> fieldChanges = ObjectCompareUtil.compareObjects(yq, yqForm);
        if (fieldChanges.size() == 0) {
            throw new SecurityException("你没有修改任何内容");
        }
        BeanUtils.copyProperties(yqForm, yq);
        updateById(yq);

        // 稽查轨迹
        List<YqJcgj> yqJcgjList = new ArrayList<>();
        for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
            YqJcgj yqJcgj = new YqJcgj();
            yqJcgj.setYqId(yq.getId());
            yqJcgj.setJcgjlx(JcgjlxEnum.bj.getValue());
            yqJcgj.setJcmc("编辑仪器");
            yqJcgj.setJcmcEn("Edit Instrument Information");
            yqJcgj.setJcmcys(JcmcysEnum.orange.getValue());
            yqJcgj.setJcnr(fieldChange.toString());
            yqJcgj.setJcnrEn(fieldChange.toEnString());
            yqJcgj.setQmrId(qmr.getUserId());
            yqJcgj.setQmrMc(qmr.getNickName());
            yqJcgj.setQmrMcEn(qmr.getUserName());
            yqJcgjList.add(yqJcgj);
        }
        yqJcgjService.saveBatch(yqJcgjList);
    }


}
