/*
 * Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
 *
 */
package net.northking.cctp.testbug.api.service;

import net.northking.cctp.common.db.BasicService;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.db.SqlOrderBy;
import net.northking.cctp.common.db.annotation.DatabaseReadOnly;
import net.northking.cctp.common.db.annotation.DatabaseWriteOnly;
import net.northking.cctp.common.db.excel.AbstractExcelService;
import net.northking.cctp.common.db.excel.ExcelHeader;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.util.UUIDUtil;
import net.northking.cctp.testbug.constants.TestBugConstants;
import net.northking.cctp.testbug.db.entity.BugLog;
import net.northking.cctp.testbug.db.service.BugLogService;
import net.northking.cctp.testbug.dto.BugLogAddDto;
import net.northking.cctp.testbug.dto.BugLogDetailDto;
import net.northking.cctp.testbug.dto.BugLogQueryDto;
import net.northking.cctp.testbug.dto.BugLogUpdateDto;
import net.northking.cctp.testbug.util.BeanHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 缺陷注释表服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

@Service
@ConditionalOnMissingBean(name = "BugLogApiServiceImpl")
public class BugLogApiServiceImpl extends AbstractExcelService<BugLog> implements BugLogApiService {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(BugLogApiServiceImpl.class);

    @Autowired
    private BugLogService bugLogService;

    //= Excel 导入导出相关代码 start ==================//
    @Override
    public BasicService<BugLog> getService() {
        return this.bugLogService;
    }

    @Override
    public ExcelHeader[] excelHeaders() {
        ExcelHeader bugLogId = new ExcelHeader("主键", "bugLogId", String.class);
        ExcelHeader bugId = new ExcelHeader("缺陷id", "bugId", String.class);
        ExcelHeader message = new ExcelHeader("注释", "message", String.class);
        ExcelHeader createBy = new ExcelHeader("创建人", "createBy", String.class);
        ExcelHeader createTime = new ExcelHeader("创建时间", "createTime", Date.class);
        ExcelHeader updateBy = new ExcelHeader("更新人", "updateBy", String.class);
        ExcelHeader updateTime = new ExcelHeader("更新时间", "updateTime", Date.class);
        ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
        return new ExcelHeader[]{};
    }

    /**
     * Excel导入，入库之前的处理
     *
     * @param records 从Excel读取到的记录
     * @return 待入库的记录
     */
    @Override
    protected List<BugLog> beforeInsertBatch(List<BugLog> records) {
        for (BugLog record : records) {
            record.setBugLogId(UUIDUtil.create32UUID());
        }
        return records;
    }

    /**
     * 写入Excel之前的处理<br>
     * 数据的特殊处理在此处加工处理
     *
     * @param record 来自数据库记录
     * @return 待写入Excel的记录
     */
    @Override
    protected BugLog beforeWriteExcel(BugLog record) {
        return record;
    }

    //= Excel 导入导出相关代码 end =======================//

    //= 增删改查 相关代码 start ==================//
    @Override
    @DatabaseReadOnly
    public Pagination<BugLogDetailDto> pagingQuery(QueryByPage<BugLogQueryDto> queryByPage) {
        return this.bugLogService.pagingQuery(queryByPage);
    }

    @Override
    @DatabaseWriteOnly
    public String add(BugLogAddDto dto) {
        String uuid = UUIDUtil.create32UUID();
        BugLog entity = new BugLog();
        BeanUtils.copyProperties(dto, entity);
        entity.setBugLogId(uuid);
        this.bugLogService.insert(entity);
        return uuid;
    }

    @Override
    @DatabaseWriteOnly
    public Integer updateByPK(BugLogUpdateDto dto) throws IllegalAccessException {
        BugLog entity = new BugLog();
        BeanUtils.copyProperties(dto, entity);
        return this.bugLogService.updateByPrimaryKey(entity);
    }

    @Override
    @DatabaseReadOnly
    public BugLogDetailDto findByPK(String uuid) {
        BugLog example = new BugLog();
        example.setBugLogId(uuid);
        List<BugLog> entitys = this.bugLogService.query(example);
        if (entitys.isEmpty()) {
            return null;
        }

        BugLogDetailDto dto = new BugLogDetailDto();
        BeanUtils.copyProperties(entitys.get(0), dto);
        return dto;
    }

    @Override
    @DatabaseWriteOnly
    public Integer deleteByPK(String uuid) {
        BugLog entity = new BugLog();
        entity.setBugLogId(uuid);
        return this.bugLogService.deleteByExample(entity);
    }

    @Override
    public List<String> addBatch(List<BugLogAddDto> batch) {
        List<String> result = new ArrayList<>();
        for (BugLogAddDto dto : batch) {
            result.add(this.add(dto));
        }
        return result;
    }

    @Override
    public Integer updateBatch(List<BugLogUpdateDto> batch) throws IllegalAccessException {
        int sum = 0;
        for (BugLogUpdateDto dto : batch) {
            sum = sum + this.updateByPK(dto);
        }
        return sum;
    }

    @Override
    public Integer deleteBatch(List<Object> batch) {
        return this.bugLogService.deleteByPrimaryKeys(batch);
    }

    /**
     * 根据bugId查询注释信息
     *
     * @param bugId
     * @return
     */
    @Override
    public List<BugLogDetailDto> queryByBugId(String bugId) {

        BugLog entity = bugLogService.createEntity();
        entity.setBugId(bugId);
        SqlOrderBy sqlOrderBy = new SqlOrderBy();
        sqlOrderBy.desc(TestBugConstants.CREATED_TIME);
        List<BugLog> bugLogs = bugLogService.query(entity, sqlOrderBy);

        return BeanHelper.copyWithCollection(bugLogs, BugLogDetailDto.class);
    }

    @Override
    public void pubAdd(BugLog bugLogAddDto) {
        bugLogService.pubAdd(bugLogAddDto);
    }

}
