package cn.com.countrygarden.bi.main.service.experience.impl;

import cn.com.countrygarden.bi.main.dao.experience.TygGzhH5Mapper;
import cn.com.countrygarden.bi.main.dao.experience.WcxExperienceMapper;
import cn.com.countrygarden.bi.main.dto.experience.WcxExperContentDto;
import cn.com.countrygarden.bi.main.dto.experience.WcxPublishContentDto;
import cn.com.countrygarden.bi.main.dto.experience.WcxPublishFileDto;
import cn.com.countrygarden.bi.main.model.experience.WcxEmployeesManage;
import cn.com.countrygarden.bi.main.service.experience.WcxExperContentService;
import cn.com.countrygarden.bi.main.util.EmptyUtils;
import com.alibaba.fastjson.JSON;
import com.vdurmont.emoji.EmojiParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class WcxExperContentServiceImpl implements WcxExperContentService {

    @Autowired
    WcxExperienceMapper wcxExperienceMapper;
    @Autowired
    private TygGzhH5Mapper tygGzhH5Mapper;

    @Override
    public List<WcxExperContentDto> queryPageList(Map<String, Object> params) throws Exception {
        log.info("1、WcxExperContentController接口请求参数："+ JSON.toJSONString(params));
        List<WcxExperContentDto> list = wcxExperienceMapper.contentList(params);
        //体验官二期添加身份查询，如果同时为客户和员工则都显示
        if (CollectionUtils.isNotEmpty(list)){
            for (WcxExperContentDto dto : list){
                String innSimpleContent = dto.getPublishContent();
                String phone = dto.getPhone();
                List<WcxEmployeesManage> wcxEmployeesManages = wcxExperienceMapper.selectByPhone(phone);
                String name="";
                boolean haveOne_0=false;
                boolean haveOne_1=false;
                for(int i=0;i<wcxEmployeesManages.size();i++){
                    WcxEmployeesManage wcxEmployeesManage = wcxEmployeesManages.get(i);
                    String identification = wcxEmployeesManage.getIdentification();
                    if("0".equals(identification)){
                        haveOne_0=true;
                    }
                    if("1".equals(identification) ){
                        haveOne_1=true;
                    }
                }
                if(haveOne_0 && haveOne_1){
                    dto.setIdentity("客户/员工");
                }else if(haveOne_0){
                    dto.setIdentity("员工");
                }else if(haveOne_1){
                    dto.setIdentity("客户");
                }
                String chatName = dto.getChatName();
                if (StringUtils.hasText(innSimpleContent)) {
                    String result = EmojiParser.parseToUnicode(innSimpleContent);
                    dto.setPublishContent(result);
                } else {
                    // todo 日志 打印没有简易信息
                }
                if (StringUtils.hasText(chatName)) {
                    String result = EmojiParser.parseToUnicode(chatName);
                    dto.setChatName(result);
                } else {
                    // todo 日志 打印没有简易信息
                }
            }
        }
        log.info("2、WcxExperContentController返回结果集："+JSON.toJSONString(list));
        return list;
    }

    @Override
    public HSSFWorkbook exportList(Map<String, Object> params) throws Exception {
        List<WcxExperContentDto> list = wcxExperienceMapper.exportList(params);

        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow row;
        HSSFCell cell;
        int rowIndex = 0;
        int cellIndex = 0;

        row = sheet.createRow(rowIndex++);

        cell = row.createCell(cellIndex++);
        cell.setCellValue("姓名");

        cell = row.createCell(cellIndex++);
        cell.setCellValue("电话");

        cell = row.createCell(cellIndex++);
        cell.setCellValue("年龄");

        cell = row.createCell(cellIndex++);
        cell.setCellValue("区域");

        cell = row.createCell(cellIndex++);
        cell.setCellValue("项目");

        cell = row.createCell(cellIndex++);
        cell.setCellValue("身份");

        cell = row.createCell(cellIndex++);
        cell.setCellValue("发布类型");

        cell = row.createCell(cellIndex++);
        cell.setCellValue("发布内容");

        cell = row.createCell(cellIndex++);
        cell.setCellValue("发布时间");

        cell = row.createCell(cellIndex++);
        cell.setCellValue("分数");

        if (CollectionUtils.isNotEmpty(list)){

            for (WcxExperContentDto dto : list){
                cellIndex = 0;
                row = sheet.createRow(rowIndex++);

                cell = row.createCell(cellIndex++);
                cell.setCellValue(dto.getName());

                cell = row.createCell(cellIndex++);
                cell.setCellValue(dto.getPhone());

                cell = row.createCell(cellIndex++);
                cell.setCellValue(EmptyUtils.isEmpty(dto.getAge())?"":String.valueOf(dto.getAge()));

                cell = row.createCell(cellIndex++);
                cell.setCellValue(dto.getOrganName());

                cell = row.createCell(cellIndex++);
                cell.setCellValue(dto.getCommName());

                cell = row.createCell(cellIndex++);
                if ("0".equals(dto.getIdentity())){
                    cell.setCellValue("员工");
                }else{
                    cell.setCellValue("客户");
                }

                cell = row.createCell(cellIndex++);
                if("0".equals(dto.getPublishType())){
                    cell.setCellValue("点赞");
                }else{
                    cell.setCellValue("吐槽");
                }

                cell = row.createCell(cellIndex++);
                cell.setCellValue(dto.getPublishContent());

                cell = row.createCell(cellIndex++);
                cell.setCellValue(dto.getPublishTime());

                cell = row.createCell(cellIndex++);
                cell.setCellValue(EmptyUtils.isEmpty(dto.getPoint())?"":String.valueOf(dto.getPoint()));

            }

        }
        return workbook;
    }

    @Override
    public int count(Map<String, Object> params) throws Exception {
        return wcxExperienceMapper.countContent(params);
    }

    @Override
    public int score(WcxExperContentDto dto) {
        //更新当前评论人总分
        int i1 = wcxExperienceMapper.selectCountSourceById(dto.getId());
        //单前评论分
        int i2 = wcxExperienceMapper.selectSourceById(dto.getId());
        //分数差
        int i = dto.getPoint() - i2;
        int i3 = i1 + i;
        tygGzhH5Mapper.updateAccountByphone(i3,dto.getId());
        return wcxExperienceMapper.score(dto);
    }

    @Override
    public int delete(WcxExperContentDto dto) {
        return wcxExperienceMapper.deleteContent(dto);
    }

    @Override
    public List<WcxPublishFileDto> getPublishFiles(WcxExperContentDto dto) {
        return wcxExperienceMapper.queryPublishFiles(dto);
    }
}
