package cn.com.ctop.kuaishou.modules.document.service.impl;

import cn.com.ctop.kuaishou.modules.document.entity.DocumentLibraryInfo;
import cn.com.ctop.kuaishou.modules.document.mapper.DocumentLibraryMapper;
import cn.com.ctop.kuaishou.modules.document.service.DocumentLibraryService;

import cn.com.ctop.kuaishou.modules.utils.ExcelUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 文档库管理
 * @Author: zzy
 * @Date: 2021-09-06
 * @Version: V1.0
 */
@Service
@Slf4j
public class DocumentLibraryServiceImpl implements DocumentLibraryService {

    @Resource
    DocumentLibraryMapper documentLibraryMapper;

    /**
     * 创建文案
     *
     * @param
     * @return
     */
    @Override
    public Result createdocument(MultipartFile file, List<String> accountIds) {
        Result result = new Result();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> documentList = new ArrayList<>();
        try {
            documentList = ExcelUtil.readExcel(file);
        } catch (Exception e) {
            log.info("读取excel错误:{}", e.toString());
        }

        try {
            if (accountIds != null && !documentList.isEmpty() && !accountIds.isEmpty()) {

                for (String docu : documentList) {
                    DocumentLibraryInfo documentLibraryInfo = new DocumentLibraryInfo();
                    documentLibraryInfo.setCopyWriter(docu);
                    documentLibraryInfo.setCopyWriterId(UUID.randomUUID().toString());
                    documentLibraryInfo.setCreateTime(formatter.format(new Date()));
                    documentLibraryInfo.setStatus(0);
                    if (docu != null && docu != "" && docu.length() <= 30) {
                        for (String accountId : accountIds) {
                            documentLibraryInfo.setAccountId(accountId);
                            documentLibraryMapper.saveDocumentLibrary(documentLibraryInfo);
                        }
                    }


                }
            }
            result.success("添加成功");
            return result;
        } catch (Exception e) {
            log.info("添加文案失败:{}", e.toString());
            result.error500("添加失败");
            return result;
        }


    }

    /**
     * 获取文案库列表
     *
     * @param
     * @return
     */
    @Override
    public Result getDocumentList(List<String> accountIds, String keywords, String startDate, String endDate, String status, int pageNumber, int pageSize) {
        Result result = new Result();
        List<JSONObject> documentLibraryList = new ArrayList<>();
        try {
            if (startDate != null && startDate != "" && endDate != null && endDate != "") {
                startDate = startDate + " 00:00:00";
                endDate = endDate + " 23:59:59";
            }
            PageHelper.startPage(pageNumber, pageSize);
            documentLibraryList = documentLibraryMapper.getDocumentLibraryList(accountIds, keywords, startDate, endDate, status);
            PageInfo pageInfo = new PageInfo(documentLibraryList);
            result.setSuccess(true);
            result.setResult(pageInfo);
        } catch (Exception e) {
            result.error500("查询数据错误");
            log.info("查询数据错误:{}", e.toString());
        }

        return result;
    }

    /**
     * 编辑文案
     *
     * @param
     * @return
     */
    @Override
    public Result updateDocument(List<String> accountIds, String copyWriterId, String copyWriter) {
        Result result = new Result();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (accountIds != null && !accountIds.isEmpty()) {
                DocumentLibraryInfo documentLibraryInfo = new DocumentLibraryInfo();
                documentLibraryInfo.setCopyWriter(copyWriter);
                documentLibraryInfo.setCopyWriterId(copyWriterId);
                documentLibraryInfo.setCreateTime(formatter.format(new Date()));
                documentLibraryInfo.setStatus(0);
                for (String accountId : accountIds) {
                    documentLibraryInfo.setAccountId(accountId);
                    List<String> dataByAccountId = documentLibraryMapper.getDataByAccountId(accountId, copyWriterId);
                    if (dataByAccountId == null || dataByAccountId.size() == 0) {
                        documentLibraryMapper.saveDocumentLibrary(documentLibraryInfo);
                    }

                }
            }
            result.success("添加成功");
        } catch (Exception e) {
            log.info("添加文案失败:{}", e.toString());
            result.error500("添加失败");
            return result;
        }

        return result;
    }

    /**
     * 文案下架
     *
     * @param
     * @return
     */
    @Override
    public Result updateStatus(String copyWriterId, String status) {
        Result result = new Result();
        try {
            documentLibraryMapper.updateStatus(copyWriterId, status);
            result.success("下架成功");
        } catch (Exception e) {
            log.info("下架失败:{}", e.toString());
            result.error500("下架失败");
        }
        return result;
    }

    /**
     * 获取被拒创意
     *
     * @param
     * @return
     */
    @Override
    public Result getRefusedList(String copyWriterId, int pageNumber, int pageSize) {
        Result result = new Result();
        List<JSONObject> documentLibraryList = new ArrayList<>();
        try {
            PageHelper.startPage(pageNumber, pageSize);
            documentLibraryList = documentLibraryMapper.getRefusedList(copyWriterId);
            PageInfo pageInfo = new PageInfo(documentLibraryList);
            result.setSuccess(true);
            result.setResult(pageInfo);
        } catch (Exception e) {
            result.error500("查询数据错误");
            log.info("查询数据错误:{}", e.toString());
        }
        return result;
    }

    /**
     * 根据文案查询绑定账户
     *
     * @param
     * @return
     */
    @Override
    public Result getAcoountId(String copyWriterId) {
        Result result = new Result();
        List<String> documentLibraryList = new ArrayList<>();
        try {
            documentLibraryList = documentLibraryMapper.getAcoountId(copyWriterId);
            result.setSuccess(true);
            result.setResult(documentLibraryList);
        } catch (Exception e) {
            result.error500("查询数据错误");
            log.info("查询数据错误:{}", e.toString());
        }
        return result;
    }
}
