/*
 * 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.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.BugManagement;
import net.northking.cctp.testbug.db.service.BugCorrelationLinkService;
import net.northking.cctp.testbug.db.entity.BugCorrelationLink;

import net.northking.cctp.testbug.db.service.BugManagementService;
import net.northking.cctp.testbug.dto.*;
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 = "BugCorrelationLinkApiServiceImpl")
public class BugCorrelationLinkApiServiceImpl extends AbstractExcelService<BugCorrelationLink> implements BugCorrelationLinkApiService {
    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(BugCorrelationLinkApiServiceImpl.class);

    @Autowired
    private BugCorrelationLinkService bugCorrelationLinkService;

    @Autowired
    private BugManagementService bugManagementService;

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

    @Override
    public ExcelHeader[] excelHeaders() {
        ExcelHeader id = new ExcelHeader("主键", "id", String.class);
        ExcelHeader bugId = new ExcelHeader("缺陷id", "bugId", String.class);
        ExcelHeader linkId = new ExcelHeader("关联id", "linkId", String.class);
        ExcelHeader linkName = new ExcelHeader("关联名", "linkName", String.class);
        ExcelHeader linkType = new ExcelHeader("关联类型：1.轮次、2.模块、3.系统、4.系统、5.案例、6.执行、7.步骤", "linkType", String.class);
        ExcelHeader createdBy = new ExcelHeader("创建人", "createdBy", String.class);
        ExcelHeader createdTime = new ExcelHeader("创建时间", "createdTime", Date.class);
        ExcelHeader updatedBy = new ExcelHeader("更新人", "updatedBy", String.class);
        ExcelHeader updatedTime = new ExcelHeader("更新时间", "updatedTime", Date.class);
        ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
        // TODO 定义表头及顺序
        return new ExcelHeader[]{};
    }

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

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

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

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

    @Override
    @DatabaseWriteOnly
    public String add(BugCorrelationLinkAddDto dto) {
        String uuid = UUIDUtil.create32UUID();
        BugCorrelationLink entity = new BugCorrelationLink();
        BeanUtils.copyProperties(dto, entity);
        entity.setId(uuid);
        this.bugCorrelationLinkService.insert(entity);
        return uuid;
    }

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

    @Override
    @DatabaseReadOnly
    public BugCorrelationLinkDetailDto findByPK(String uuid) {
        BugCorrelationLink example = new BugCorrelationLink();
        example.setId(uuid);
        List<BugCorrelationLink> entitys = this.bugCorrelationLinkService.query(example);
        if (entitys.isEmpty()) {
            return null;
        }

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

    @Override
    @DatabaseWriteOnly
    public Integer deleteByPK(String uuid) {
        BugCorrelationLink entity = new BugCorrelationLink();
        entity.setId(uuid);
        return this.bugCorrelationLinkService.deleteByExample(entity);
    }

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

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

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

    /**
     * 根据bugId查询关联信息
     *
     * @param bugId
     * @return
     */
    @Override
    public List<BugCorrelationLinkDetailDto> queryByBugId(String bugId) {
        //缺陷类型
        BugManagement byPrimaryKey = bugManagementService.findByPrimaryKey(bugId);
        BugCorrelationLink entity = bugCorrelationLinkService.createEntity();
        entity.setBugId(bugId);
        if (TestBugConstants.STR_TWO.equals(byPrimaryKey.getBugFrom())) {
            entity.setLinkType(TestBugConstants.STR_FOUR);
        } else {
            entity.setLinkType(TestBugConstants.STR_ONE);
        }
        List<BugCorrelationLink> query = bugCorrelationLinkService.query(entity);

        return BeanHelper.copyWithCollection(query, BugCorrelationLinkDetailDto.class);
    }

    /**
     * 查询关联用例信息
     *
     * @param tenantId
     * @return
     */
    @Override
    public List<BugCorrelationLinkDetailDto> queryByTenantId(String tenantId) {
        BugCorrelationLink entity = bugCorrelationLinkService.createEntity();
        entity.setTenantId(tenantId);
        entity.setLinkType(TestBugConstants.STR_ONE);
        List<BugCorrelationLink> query = bugCorrelationLinkService.query(entity);
        return BeanHelper.copyWithCollection(query, BugCorrelationLinkDetailDto.class);
    }

    /**
     * 查询缺陷关联信息
     *
     * @param linkId 关联id
     * @return
     */
    @Override
    public List<BugCorrelationLink> listQuery(String linkId) {
        BugCorrelationLink bugCorrelationLink = new BugCorrelationLink();
        bugCorrelationLink.setLinkId(linkId);
        return bugCorrelationLinkService.query(bugCorrelationLink);
    }

    @Override
    public void pubAdd(BugCorrelationLink addDto) {
        bugCorrelationLinkService.pubAdd(addDto);
    }

    @Override
    public Integer removeByCode(BugLinkDeleteDto batch) {
        if (batch == null || batch.getBugCode() == null) {
            return null;
        }
        Integer count = bugCorrelationLinkService.removeByCode(batch.getBugCode());
        return count;
    }

}
