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

import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.cache.EntityNameCatalog;
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.dw.constant.DWBaseConstant;
import net.northking.cctp.dw.db.service.DwCaseDesignInfoService;
import net.northking.cctp.dw.db.entity.DwCaseDesignInfo;

import net.northking.cctp.dw.dto.*;
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;

/**
 * dw用例表服务实现类
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */

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

    @Autowired
    private DwCaseDesignInfoService dwCaseDesignInfoService;

    @Autowired
    private EntityNameCache entityNameCache;

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

    @Override
    public ExcelHeader[] excelHeaders() {
        ExcelHeader id = new ExcelHeader("用例主键", "id", String.class);
        ExcelHeader projectId = new ExcelHeader("所属系统id", "projectId", String.class);
        ExcelHeader createdTime = new ExcelHeader("创建时间", "createdTime", Date.class);
        ExcelHeader createdBy = new ExcelHeader("创建人", "createdBy", String.class);
        ExcelHeader tenantId = new ExcelHeader("租户id", "tenantId", String.class);
        // TODO 定义表头及顺序
        return new ExcelHeader[]{};
    }

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

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

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

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

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

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

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

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

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

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

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

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

    @Override
    public List<MtCaseDesignChartDetailDto> listQuery(DwCaseDesignInfoQueryDto query) {
        if (query.getStartTime() != null && !query.getStartTime().isEmpty() && query.getEndTime() != null && !query.getEndTime().isEmpty()) {
            query.setStartTime(query.getStartTime() + DWBaseConstant.CASE_START_TIME);
            query.setEndTime(query.getEndTime() + DWBaseConstant.CASE_END_TIME);
        }
        List<MtCaseDesignChartDetailDto> resultList = dwCaseDesignInfoService.listQuery(query);
        if (!resultList.isEmpty()) {
            for(MtCaseDesignChartDetailDto item:resultList){
                item.setCreateBy(entityNameCache.translateById(EntityNameCatalog.WITH_LOGIN_NO, item.getCreateBy()));
            }
        }
        return resultList;
    }

}
