package com.hxhq.business.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.Bacteria;
import com.hxhq.business.enums.zykgl.JcgjlxEnum;
import com.hxhq.business.enums.zykgl.JcmcysEnum;
import com.hxhq.business.form.gsp.CzForm;
import com.hxhq.business.form.study.StudyCellSearchForm;
import com.hxhq.business.service.ICellJcgjService;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.system.api.domain.SysUser;
import com.hxhq.system.service.ISysUserService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.CellMapper;
import com.hxhq.business.domain.Cell;
import com.hxhq.business.service.ICellService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 细胞Service业务层处理
 *
 * @author hxhq
 * @date 2026-01-12
 */
@Service
public class CellServiceImpl extends ServiceImpl<CellMapper, Cell> implements ICellService
{
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ICellJcgjService cellJcgjService;

    @Override
    public List<Cell> queryStudyList(StudyCellSearchForm form) {
        QueryWrapper<Cell> queryWrapper = Wrappers.query();
        queryWrapper.eq("sc.del_flag", 0)
                .eq("c.del_flag", 0);
        if(form.getStudyId() != null) {
            queryWrapper.eq("sc.study_id", form.getStudyId());
        }
        if(StringUtils.isNotEmpty(form.getMc())) {
            queryWrapper.like("c.mc", form.getMc());
        }
        if(StringUtils.isNotEmpty(form.getBh())) {
            queryWrapper.like("c.bh", form.getBh());
        }
        if(StringUtils.isNotEmpty(form.getLy())) {
            queryWrapper.like("c.ly", form.getLy());
        }
        if(StringUtils.isNotEmpty(form.getStartDate())) {
            queryWrapper.ge("c.sxr", form.getStartDate());
        }
        if(StringUtils.isNotEmpty(form.getEndDate())) {
            queryWrapper.le("c.sxr", form.getEndDate());
        }
        if(StringUtils.isNotEmpty(form.getStudyName())) {
            queryWrapper.like("t.name", form.getStudyName());
        }
        queryWrapper.orderByDesc("c.bh");
        return baseMapper.queryStudyList(queryWrapper);
    }

    @Override
    public void cz(CzForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), true);

        Cell cell = this.getById(form.getId());
        if(cell == null) {
            throw new ServiceException("细胞不存在或已删除");
        }
        if(!NumberUtils.isParsable(form.getCzl())) {
            throw new ServiceException("请输入正确的处置量");
        }
        BigDecimal tj = new BigDecimal(cell.getTj());
        BigDecimal czl = new BigDecimal(form.getCzl());
        tj = tj.subtract(czl);
        cell.setTj(tj.toString());
        this.updateById(cell);


        cellJcgjService.saveJcgj(cell.getId(), JcgjlxEnum.lc.getValue(), "处置", JcmcysEnum.blue.getValue(), null, qmr.getUserId(), qmr.getNickName());
    }
}
