package com.hzya.frame.grpU8.nxproof.oerannex.service.impl;/*
* @Description 附件关系表
* @Author xiangerlin 
  @Date 2022-10-26 10:07
*/

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.grpU8.nxproof.oerannex.dao.IOerAnnexDao;
import com.hzya.frame.grpU8.nxproof.oerannex.entity.OerAnnexEntity;
import com.hzya.frame.grpU8.nxproof.oerannex.service.IOerAnnexService;
import com.hzya.frame.web.exception.BaseSystemException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class OerAnnexServiceImpl extends BaseService<OerAnnexEntity,String> implements IOerAnnexService {

    Logger logger = LogManager.getLogger(getClass());

    protected IOerAnnexDao oerAnnexDao;

    @Autowired
    public void setBillLogDao(IOerAnnexDao dao) {
        this.oerAnnexDao = dao;
        this.dao=dao;
    }

    /**
     * 查询附件关系
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    public List<OerAnnexEntity> queryAnnex(OerAnnexEntity entity) throws Exception {
        List<OerAnnexEntity> annexList = oerAnnexDao.query(entity);
        return annexList;
    }

    /**
     * 删除附件关系
     *
     * @param entity
     * @throws Exception
     */
    @Override
    public void deleteAnnex(OerAnnexEntity entity){
        if (null != entity && StrUtil.isNotEmpty(entity.getMlid()) && StrUtil.isNotEmpty(entity.getKjnd())){
            oerAnnexDao.delete("OerAnnexEntity_delete",entity);
        }
    }

    /**
     * 查询最大id
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    public String queryMaxId(OerAnnexEntity entity) throws Exception {
        if (null == entity){
            entity = new OerAnnexEntity();
        }
        if (StrUtil.isEmpty(entity.getKjnd())){
            entity.setKjnd(String.valueOf(DateUtil.year(new Date())));
        }
        OerAnnexEntity max = oerAnnexDao.query(entity,"OerAnnexEntity_max_id");
        if (null != max){
            Long max_value = Long.valueOf(max.getMaxAnnexid());
            return String.valueOf(max_value+1);
        }
        return null;
    }

    /**
     * 保存附件关系
     * @param entity
     * @throws Exception
     */
    @Override
    public void saveAnnex(OerAnnexEntity entity) throws Exception {
        if (null == entity){
            throw new BaseSystemException("参数不能为空");
        }
        //entity.setAcc_name(Constant.CONFIGMAP.get("file_acc_name"));
        entity.setAnnexid(queryMaxId(entity));
        if (null != entity.getAnnexSize()){
            int fileSize = entity.getAnnexSize().intValue();
            String showSize = "";
            if (fileSize < 1000) {
                showSize = fileSize + "b";
            } else if (1000 < fileSize && fileSize < 1000000) {
                showSize = (fileSize / 1000) + " KB";
            } else {
                showSize = (fileSize / 1000000) + " MB";
            }
            entity.setShowSize(showSize);
        }
       /* if (!entity.getAnnexName().contains("合同")) {
            entity.setFjType("其它");
        }else {
            entity.setFjType("合同");
        }*/
        entity.setFjType("其它");
        oerAnnexDao.save(entity);
    }

    /**
     * 查询附件关系，联查附件对象表
     *
     * @param mlid
     * @return
     * @throws Exception
     */
    @Override
    public List<OerAnnexEntity> queryAnnexAndFile(String mlid) throws Exception {
        OerAnnexEntity entity = new OerAnnexEntity();
        entity.setMlid(mlid);
        List<OerAnnexEntity> oerAnnexList = oerAnnexDao.queryList(entity, "OerAnnexEntity_list_base_join_attachment");
        return oerAnnexList;
    }
}
