package com.zt.plat.module.base.service.base;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.api.dto.ElementDTO;
import com.zt.plat.module.base.controller.admin.base.vo.ElementPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.ElementRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.ElementSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.base.ElementDO;
import com.zt.plat.module.base.dal.mysql.base.ElementMapper;
import jakarta.annotation.Resource;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.base.enums.ErrorCodeConstants.ELEMENT_EXISTS;
import static com.zt.plat.module.base.enums.ErrorCodeConstants.ELEMENT_NOT_EXISTS;

/**
 * 金属元素 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ElementServiceImpl implements ElementService {

    @Resource
    private ElementMapper elementMapper;

    @Override
    public ElementRespVO createElement(ElementSaveReqVO createReqVO) {
        // 插入
        ElementDO element = BeanUtils.toBean(createReqVO, ElementDO.class);
        // 校验存在
        validateElementCodeExists(createReqVO.getAbbreviation(),"insert",null);
        //金属编码自动生成，格式 JSYS-00001,依次新增
        String maxCode = elementMapper.selectMaxCode();
        if (maxCode == null) {
            element.setCoding("JSYS-00001");
        } else {
            String prefix = "JSYS-";
            String numberPart = maxCode.substring(prefix.length());
            int nextNumber = Integer.parseInt(numberPart) + 1;
            String nextCode = prefix + String.format("%05d", nextNumber);
            element.setCoding(nextCode);
        }
        elementMapper.insert(element);
        // 返回
        return BeanUtils.toBean(element, ElementRespVO.class);
    }

    @Override
    public void updateElement(ElementSaveReqVO updateReqVO) {
        // 校验存在
        validateElementExists(updateReqVO.getId());
        // 校验存在
        validateElementCodeExists(updateReqVO.getAbbreviation(),"update",updateReqVO.getId());
        // 更新
        ElementDO updateObj = BeanUtils.toBean(updateReqVO, ElementDO.class);
        elementMapper.updateById(updateObj);
    }

    @Override
    public void deleteElement(Long id) {
        // 校验存在
        validateElementExists(id);
        // 删除
        elementMapper.deleteById(id);
    }

    @Override
    public void deleteElementListByIds(List<Long> ids) {
        // 校验存在
        validateElementExists(ids);
        // 删除
        elementMapper.deleteByIds(ids);
    }

    private void validateElementExists(List<Long> ids) {
        List<ElementDO> list = elementMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(ELEMENT_NOT_EXISTS);
        }
    }

    private void validateElementExists(Long id) {
        if (elementMapper.selectById(id) == null) {
            throw exception(ELEMENT_NOT_EXISTS);
        }
    }

    private void validateElementCodeExists(String code,String type,Long id) {
        ElementDO elementDO =  elementMapper.getElementName(code);
        if (elementDO != null&&type.equals("insert")) {
            throw exception(ELEMENT_EXISTS);
        }else if (elementDO != null&&type.equals("update")&& !elementDO.getId().equals(id)) {
            throw exception(ELEMENT_EXISTS);
        }
    }

    @Override
    public ElementDO getElement(Long id) {
        return elementMapper.selectById(id);
    }

    @Override
    public PageResult<ElementDO> getElementPage(ElementPageReqVO pageReqVO) {
        return elementMapper.selectPage(pageReqVO);
    }

    @Override
    public void enableElementList(List<ElementRespVO> saveReqVOS) {
        List<ElementDO> updateObj = BeanUtils.toBean(saveReqVOS, ElementDO.class);
        List<BatchResult> count = elementMapper.updateById(updateObj);
        if (CollUtil.isEmpty(count)) {
            throw exception(ELEMENT_NOT_EXISTS);
        }
    }

    @Override
    public List<ElementDO> getElementNoPage() {
        return elementMapper.getElementNoPage();
    }

    @Override
    public PageResult<ElementDO> getElementPageByEnable(ElementPageReqVO pageReqVO) {
        if (pageReqVO!=null&&pageReqVO.getIsEnable()==null){
            pageReqVO.setIsEnable("1");
        }
        return elementMapper.selectPage(pageReqVO);
    }

    @Override
    public ElementDO getElementBySymbol(String symbol) {
        return elementMapper.selectFirstOne(ElementDO::getAbbreviation, symbol);
    }

}