package com.iformall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.dto.WxCUserBasicInfoDto;
import com.iformall.domain.po.WxCUser;
import com.iformall.domain.po.WxCUserBasicInfo;
import com.iformall.domain.po.WxCarPayRecord;
import com.iformall.domain.po.WxTags;
import com.iformall.domain.vo.CUserBaseInfoT;
import com.iformall.domain.vo.CUserBaseVo;
import com.iformall.domain.vo.CUserTagVo;
import com.iformall.domain.vo.WxCUserBasicInfoVo;
import com.iformall.enums.EnumScoreType;
import com.iformall.mapper.*;
import com.iformall.service.WxCUserBasicInfoService;
import com.iformall.service.WxCUserTagsService;
import com.iformall.service.WxScoreRulesService;
import com.iformall.utils.Constant;
import com.iformall.utils.DownFileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class WxCUserBasicInfoServiceImpl implements WxCUserBasicInfoService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxCUserTagsMapper wxCUserTagsMapper;

    @Autowired
    WxCUserBasicInfoMapper wxCUserBasicInfoMapper;

    @Autowired
    WxCUserMapper wxCUserMapper;

    @Autowired
    WxTagsMapper wxTagsMapper;

    @Autowired
    WxCUserTagsService wxCUserTagsService;

    @Autowired
    WxScoreRulesService wxScoreRulesService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    WxCarPayRecordMapper wxCarPayRecordMapper;

    @Override
    public PageInfo<WxCUserBasicInfo> listAsPage(WxCUserBasicInfo record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxCUserBasicInfoMapper.findList(record));
    }

    @Override
    public int cntUserList(WxCUserBasicInfo record) {
        return wxCUserBasicInfoMapper.selectCount(record);
    }

    @Override
    public List<WxCUserBasicInfo> findByPhone(String tenantId, String phone) {
        WxCUserBasicInfo basicQ = new WxCUserBasicInfo();
        basicQ.setTenantId(tenantId);
        basicQ.setPhone(phone);
        return wxCUserBasicInfoMapper.select(basicQ);
    }

    @Override
    public void updateScore(WxCUserBasicInfo record) {
        wxCUserBasicInfoMapper.updateScore(record);
    }

    @Override
    public WxCUserBasicInfo getById(Long id) {
        return wxCUserBasicInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(WxCUserBasicInfo record) {
        wxCUserBasicInfoMapper.insertSelective(record);
    }

    @Override
    public void update(WxCUserBasicInfo record) {
        wxCUserBasicInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void updateObj(WxCUserBasicInfo record, Long newId) {
        // update c_user_tags
        CUserTagVo userTagVo = new CUserTagVo();
        userTagVo.setTenantId(record.getTenantId());
        userTagVo.setUserId(record.getId());
        userTagVo.setNewUserId(newId);
        wxCUserTagsMapper.updateNewId(userTagVo);
        // base info
        CUserBaseVo userBaseVo = new CUserBaseVo();
        org.springframework.beans.BeanUtils.copyProperties(record, userBaseVo);
        userBaseVo.setNewId(newId);
        wxCUserBasicInfoMapper.updateNewId(userBaseVo);
    }

    @Override
    public void deleteById(Long id) {
        wxCUserBasicInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public long findCountBySex(WxCUserBasicInfoDto dto) {
        return wxCUserBasicInfoMapper.findCountBySex(dto);
    }

    @Override
    public long findCountByAge(WxCUserBasicInfoDto dto) {
        return wxCUserBasicInfoMapper.findCountByAge(dto);
    }

    @Override
    public long findCountByScore(WxCUserBasicInfoDto dto) {
        return wxCUserBasicInfoMapper.findCountByScore(dto);
    }

    @Override
    public int  exportDataCount(WxCUserBasicInfo basicInfo) {
        return wxCUserBasicInfoMapper.countVoList(basicInfo);
    }

    @Override
    public void exportData(WxCUserBasicInfo  basicInfo, HttpServletRequest request, HttpServletResponse response) {
        List<WxCUserBasicInfoVo> memberlist = wxCUserBasicInfoMapper.findVoList(basicInfo);

        SXSSFWorkbook workbook;
        String filepath = Constant.fileDirectory;
        File savefile = new File(filepath);
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        String filename = UUID.randomUUID() + ".xlsx";
        filepath = filepath + filename;

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        WxTags wxTagsQ = new WxTags();
        List<WxTags> tagList = wxTagsMapper.findList(wxTagsQ);

        List<Long> tagIdList = new ArrayList<Long>();

        try {
            File file = new File(filepath);
            workbook = new SXSSFWorkbook(100);
            SXSSFSheet sheetTwo = workbook.createSheet("会员信息");
            Row rowtwo = sheetTwo.createRow(0);
            Cell nameCellTwo = rowtwo.createCell(0);
            Cell sexCellTwo = rowtwo.createCell(1);
            Cell phoneCellTwo = rowtwo.createCell(2);
            Cell nickNameCellTwo = rowtwo.createCell(3);
            Cell educationCellTwo = rowtwo.createCell(4);
            Cell birthdateCellTwo = rowtwo.createCell(5);
            Cell addrCellTwo = rowtwo.createCell(6);
            Cell updateDateCellTwo = rowtwo.createCell(7);
            Cell createDateCellTwo = rowtwo.createCell(8);
            Cell tagCellTwo = rowtwo.createCell(9);
            Cell scoreCellTwo = rowtwo.createCell(10);

            nameCellTwo.setCellValue("姓名");
            sexCellTwo.setCellValue("性别");
            phoneCellTwo.setCellValue("手机号");
            nickNameCellTwo.setCellValue("微信昵称");
            educationCellTwo.setCellValue("学历");
            birthdateCellTwo.setCellValue("生日");
            addrCellTwo.setCellValue("地址");
            updateDateCellTwo.setCellValue("上次活跃时间");
            createDateCellTwo.setCellValue("注册时间");
            tagCellTwo.setCellValue("标签");
            scoreCellTwo.setCellValue("成长值");

            for (int i = 0; i < memberlist.size(); i++) {
                rowtwo = sheetTwo.createRow(i + 1);
                nameCellTwo = rowtwo.createCell(0);
                sexCellTwo = rowtwo.createCell(1);
                phoneCellTwo = rowtwo.createCell(2);
                nickNameCellTwo = rowtwo.createCell(3);
                educationCellTwo = rowtwo.createCell(4);
                birthdateCellTwo = rowtwo.createCell(5);
                addrCellTwo = rowtwo.createCell(6);
                updateDateCellTwo = rowtwo.createCell(7);
                createDateCellTwo = rowtwo.createCell(8);
                tagCellTwo = rowtwo.createCell(9);
                scoreCellTwo = rowtwo.createCell(10);

                WxCUserBasicInfoVo entity = memberlist.get(i);

                String sexStr = "保密";
                if (entity.getSex() == 1)
                    sexStr = "男";
                else if (entity.getSex() == 2)
                    sexStr = "女";
                nameCellTwo.setCellValue(entity.getName());
                sexCellTwo.setCellValue(sexStr);
                phoneCellTwo.setCellValue(entity.getPhone());
                nickNameCellTwo.setCellValue(entity.getNickName());
                educationCellTwo.setCellValue(entity.getEducation());
                if (entity.getBirthdate() != null)
                    birthdateCellTwo.setCellValue(sdf.format(entity.getBirthdate()));
                addrCellTwo.setCellValue(entity.getAddressStr());
                updateDateCellTwo.setCellValue(sdf.format(entity.getUpdateDate()));
                createDateCellTwo.setCellValue(sdf.format(entity.getCreateDate()));
                if (entity != null) {
                    if (entity.getTagId() != null && entity.getTags() != null) {
                        if (StringUtils.isNotBlank(entity.getTags())) {
                            List<Long> ids = JSONObject.parseArray(entity.getTags(), Long.class);
                            if (!ids.isEmpty()) {
                                String tagNames = "";
                                String tagIds = "";
                                tagIdList.clear();
                                for(Long id: ids) {
                                    for(WxTags tag: tagList) {
                                        if(id.equals(tag.getId())) {
                                            tagNames += tag.getName() + "/";
                                            tagIds += tag.getId() + ",";
                                            tagIdList.add(tag.getId());
                                            break;
                                        }
                                    }
                                }
                                if (StringUtils.isNotBlank(tagNames)) {
                                    entity.setTagNames(tagNames.substring(0, tagNames.length() - 1));
                                    tagCellTwo.setCellValue(entity.getTagNames());
                                }
                                if (StringUtils.isNoneBlank(tagIds)) {
                                    entity.setTagIds(tagIds.substring(0, tagIds.length() - 1));
                                }
                            }
                        }
                    }
                }
                // 成长值
                if (entity.getPoins() != null) {
                    scoreCellTwo.setCellValue(entity.getPoins());
                }
            }


            FileOutputStream fileOut = new FileOutputStream(file);
            workbook.write(fileOut);
            fileOut.close();
            workbook.close();
            DownFileUtil.downFile(filepath,"会员信息数据.xlsx",response, request);
            FileUtils.forceDelete(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportTemplate(HttpServletRequest request, HttpServletResponse response, String tenantId) {

        SXSSFWorkbook workbook;
        String filepath = Constant.fileDirectory;
        File savefile = new File(filepath);
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        String filename = UUID.randomUUID() + ".xlsx";
        filepath = filepath + filename;
        try {
            File file = new File(filepath);
            workbook = new SXSSFWorkbook(100);

            SXSSFSheet sheetTwo = workbook.createSheet("会员信息");

            Row rowtwo = sheetTwo.createRow(0);

            Cell nameCellTwo = rowtwo.createCell(0);
            Cell sexCellTwo = rowtwo.createCell(1);
            Cell phoneCellTwo = rowtwo.createCell(2);
            Cell nickNameCellTwo = rowtwo.createCell(3);
            Cell educationCellTwo = rowtwo.createCell(4);
            Cell birthdateCellTwo = rowtwo.createCell(5);
            Cell addrCellTwo = rowtwo.createCell(6);
            Cell updateDateCellTwo = rowtwo.createCell(7);
            Cell createDateCellTwo = rowtwo.createCell(8);
            Cell tagCellTwo = rowtwo.createCell(9);
            Cell scoreCellTwo = rowtwo.createCell(10);

            nameCellTwo.setCellValue("姓名");
            sexCellTwo.setCellValue("性别");
            phoneCellTwo.setCellValue("手机号");
            nickNameCellTwo.setCellValue("微信昵称");
            educationCellTwo.setCellValue("学历");
            birthdateCellTwo.setCellValue("生日");
            addrCellTwo.setCellValue("地址");
            updateDateCellTwo.setCellValue("上次活跃时间");
            createDateCellTwo.setCellValue("注册时间");
            tagCellTwo.setCellValue("标签");
            scoreCellTwo.setCellValue("成长值");
            {
                rowtwo = sheetTwo.createRow(1);
                nameCellTwo = rowtwo.createCell(0);
                sexCellTwo = rowtwo.createCell(1);
                phoneCellTwo = rowtwo.createCell(2);
                nickNameCellTwo = rowtwo.createCell(3);
                educationCellTwo = rowtwo.createCell(4);
                birthdateCellTwo = rowtwo.createCell(5);
                addrCellTwo = rowtwo.createCell(6);
                updateDateCellTwo = rowtwo.createCell(7);
                createDateCellTwo = rowtwo.createCell(8);
                tagCellTwo = rowtwo.createCell(9);
                scoreCellTwo = rowtwo.createCell(10);
                nameCellTwo.setCellValue("例子1");
                sexCellTwo.setCellValue("保密");
                phoneCellTwo.setCellValue("13900010001");
                nickNameCellTwo.setCellValue("昵称");
                educationCellTwo.setCellValue("本科");
                birthdateCellTwo.setCellValue("2018-09-10");
                addrCellTwo.setCellValue("地址");
                updateDateCellTwo.setCellValue("2018-08-10");
                createDateCellTwo.setCellValue("2018-08-10");
                tagCellTwo.setCellValue("附近住户");
                scoreCellTwo.setCellValue("100");
            }
            {
                rowtwo = sheetTwo.createRow(2);
                nameCellTwo = rowtwo.createCell(0);
                sexCellTwo = rowtwo.createCell(1);
                phoneCellTwo = rowtwo.createCell(2);
                nickNameCellTwo = rowtwo.createCell(3);
                educationCellTwo = rowtwo.createCell(4);
                birthdateCellTwo = rowtwo.createCell(5);
                addrCellTwo = rowtwo.createCell(6);
                updateDateCellTwo = rowtwo.createCell(7);
                createDateCellTwo = rowtwo.createCell(8);
                tagCellTwo = rowtwo.createCell(9);
                nameCellTwo.setCellValue("例子2");
                sexCellTwo.setCellValue("男");
                phoneCellTwo.setCellValue("13900020002");
                nickNameCellTwo.setCellValue("昵称");
                educationCellTwo.setCellValue("本科");
                birthdateCellTwo.setCellValue("2018-09-10");
                addrCellTwo.setCellValue("地址");
                updateDateCellTwo.setCellValue("2018-08-10");
                createDateCellTwo.setCellValue("2018-08-10");
                tagCellTwo.setCellValue("男/附近住户");
                scoreCellTwo.setCellValue("200");
            }
            {
                rowtwo = sheetTwo.createRow(3);
                nameCellTwo = rowtwo.createCell(0);
                sexCellTwo = rowtwo.createCell(1);
                phoneCellTwo = rowtwo.createCell(2);
                nickNameCellTwo = rowtwo.createCell(3);
                educationCellTwo = rowtwo.createCell(4);
                birthdateCellTwo = rowtwo.createCell(5);
                addrCellTwo = rowtwo.createCell(6);
                updateDateCellTwo = rowtwo.createCell(7);
                createDateCellTwo = rowtwo.createCell(8);
                tagCellTwo = rowtwo.createCell(9);
                nameCellTwo.setCellValue("例子3");
                sexCellTwo.setCellValue("女");
                phoneCellTwo.setCellValue("13900030003");
                nickNameCellTwo.setCellValue("昵称");
                educationCellTwo.setCellValue("本科");
                birthdateCellTwo.setCellValue("2018-09-10");
                addrCellTwo.setCellValue("地址");
                updateDateCellTwo.setCellValue("2018-08-10");
                createDateCellTwo.setCellValue("2018-08-10");
                tagCellTwo.setCellValue("女/附近住户");
                scoreCellTwo.setCellValue("110");
            }

            FileOutputStream fileOut = new FileOutputStream(file);
            workbook.write(fileOut);
            fileOut.close();
            workbook.close();
            DownFileUtil.downFile(filepath, "会员信息模板.xlsx",response, request);
            FileUtils.forceDelete(file);
        } catch (Exception e) {
            logger.error("导出模板:" + e.getMessage());
        }

    }


    public WxCUserBasicInfo InitUserBaseInfo(CUserBaseInfoT uBase, String tenantId) {
        if (StringUtils.isBlank(uBase.getPhone())) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        WxCUserBasicInfo userBase = new WxCUserBasicInfo();

        userBase.setTenantId(tenantId);
        userBase.setName(uBase.getName());
        if (uBase.getSex() == null) {
            userBase.setSex(0);
        } else {
            userBase.setSex(uBase.getSex());
        }

        userBase.setPhone(uBase.getPhone());
        userBase.setNickName(uBase.getNickName());
        userBase.setEducation(uBase.getEducation());
        if (uBase.getBirthdate() != null) {
            try {
                userBase.setBirthdate(sdf.parse(uBase.getBirthdate()));
            } catch (ParseException e) {
                logger.error("生日： " + e.getMessage());
            }
        }

        userBase.setAddress(uBase.getAddress());
        if (uBase.getUpdateDate() != null) {
            try {
                userBase.setUpdateDate(sdf.parse(uBase.getUpdateDate()));
            } catch (ParseException e) {
                logger.error("上次活跃时间： " + e.getMessage());
            }
        }
        if (uBase.getCreateDate() != null) {
            try {
                userBase.setCreateDate(sdf.parse(uBase.getCreateDate()));
            } catch (ParseException e) {
                logger.error("注册时间： " + e.getMessage());
            }
        }

        userBase.setTagNames(uBase.getTagNames());

        return userBase;
    }

    @Override
    public void importOneMem(String tenantId, String importKey, List<WxTags> tagList, CUserBaseInfoT uBase) {
        final IdWorker idWorker = IdWorker.get();

        if (StringUtils.isBlank(uBase.getPhone())) {
            stringRedisTemplate.opsForHash().increment(importKey, "processCount", 1);
            logger.error("手机号为空", uBase.toString());
            return;
        }
        WxCUserBasicInfo userBase = InitUserBaseInfo(uBase, tenantId);
        if (userBase == null) {
            stringRedisTemplate.opsForHash().increment(importKey, "processCount", 1);
            logger.error("userBase为null", uBase.toString());
            return;
        }

        boolean bHaveCUser = false;

        // 成长值
        Integer points = null;
        if (StringUtils.isNotBlank(uBase.getPoins())) {
            try {
                points = Integer.valueOf(uBase.getPoins());
                userBase.setPoins(points);
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }

        WxCUserBasicInfo oldUserBase = null;
        WxCUserBasicInfo userBaseQ = new WxCUserBasicInfo();
        userBaseQ.setTenantId(tenantId);
        userBaseQ.setPhone(userBase.getPhone());
        List<WxCUserBasicInfo> userBList = wxCUserBasicInfoMapper.select(userBaseQ);
        if (userBList.size() > 0) {
            oldUserBase = userBList.get(0);
        }

        if (oldUserBase != null) {
            oldUserBase.setName(userBase.getName());
            oldUserBase.setSex(userBase.getSex());
            oldUserBase.setNickName(userBase.getNickName());
            oldUserBase.setEducation(userBase.getEducation());
            oldUserBase.setBirthdate(userBase.getBirthdate());
            oldUserBase.setAddress(userBase.getAddress());
            Date updateDate = oldUserBase.getUpdateDate() == null ? new Date() : oldUserBase.getUpdateDate();
            Date createDate = oldUserBase.getCreateDate() == null ? new Date() : oldUserBase.getCreateDate();
            oldUserBase.setUpdateDate(userBase.getUpdateDate() == null ? updateDate : userBase.getUpdateDate());
            oldUserBase.setCreateDate(userBase.getCreateDate() == null ? createDate : userBase.getCreateDate());
            // 成长值
            if(points != null) {
                oldUserBase.setPoins(points);
            }
        } else {
            userBase.setId(idWorker.nextId());
            userBase.setUpdateDate(userBase.getUpdateDate() == null ? new Date() : userBase.getUpdateDate());
            userBase.setCreateDate(userBase.getCreateDate() == null ? new Date() : userBase.getCreateDate());
        }

        // update 昵称
        {
            // check c_user 是否存在
            WxCUser cUserQ = new WxCUser();
            cUserQ.setTenantId(tenantId);
            cUserQ.setPhone(userBase.getPhone());
            List<WxCUser> userList = wxCUserMapper.select(cUserQ);
            if (userList.size() > 0) {
                WxCUser user = userList.get(0);
                if (user != null) {
                    bHaveCUser = true;
                    if (oldUserBase != null) {
                        if(oldUserBase != null) {
                            oldUserBase.setNickName(user.getNickName());
                        }
                        userBase.setId(user.getId());
                        userBase.setNickName(user.getNickName());
                    } else {
                        userBase.setNickName(user.getNickName());
                    }
                }
            }
        }


        if (oldUserBase != null) {
            logger.info("update user base: " + oldUserBase.getId());
            wxCUserBasicInfoMapper.updateByPrimaryKeySelective(oldUserBase);
        } else {
            logger.info("insert user base: " + userBase.getId());
            try {
                wxCUserBasicInfoMapper.insertSelective(userBase);
            } catch (Exception e) {
                stringRedisTemplate.opsForHash().increment(importKey, "processCount", 1);
                logger.error(e.getMessage());
                return;
            }
        }
        // update tags
        try {
            if (StringUtils.isNotBlank(userBase.getTagNames())) {
                // get tag ids
                String tagNames = userBase.getTagNames();
                List<Long> tagIdList = new ArrayList<>();
                for (String tagName : tagNames.split("/")) {
                    for(WxTags tag: tagList) {
                        if(tagName.equals(tag.getName())) {
                            tagIdList.add(Long.valueOf(tag.getId()));
                            break;
                        }
                    }
                }

                if (oldUserBase != null){
                    wxCUserTagsService.assignTags(tagIdList,oldUserBase);
                }else{
                    wxCUserTagsService.assignTags(tagIdList,userBase);
                }
            }

            //触发自动打标签, 晚上会自动打标签，所以去掉导入打标签
            /*
            if (oldUserBase != null) {
                wxCUserTagsService.triggerAssignTags(EnumAssignTagsTrigger.ASSIGN_TAGS_TRIGGER_IMPORT,oldUserBase);
            }
            else{
                wxCUserTagsService.triggerAssignTags(EnumAssignTagsTrigger.ASSIGN_TAGS_TRIGGER_IMPORT,userBase);
            }
            */

            // 成长值
            if (points != null) {
                if (oldUserBase != null) {
                    wxScoreRulesService.addScore2(EnumScoreType.MEM_IMPORT, oldUserBase, bHaveCUser);
                } else {
                    wxScoreRulesService.addScore2(EnumScoreType.MEM_IMPORT, userBase, bHaveCUser);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            logger.error("打标签+成长值失败--继续导入");
        }

        //记数
        stringRedisTemplate.opsForHash().increment(importKey,"processCount",1);
        stringRedisTemplate.expire(importKey,10, TimeUnit.SECONDS);
    }

    @Override
    public long findCount(WxCUserBasicInfoDto dto) {
        return wxCUserBasicInfoMapper.findCount(dto);
    }

    @Override
    public void exportCarPayData(WxCarPayRecord record, HttpServletRequest request, HttpServletResponse response) {
        List<WxCarPayRecord> carPayList = wxCarPayRecordMapper.findCarPayList(record);

        SXSSFWorkbook workbook;
        String filepath = Constant.fileDirectory;
        File savefile = new File(filepath);
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        String filename = UUID.randomUUID() + ".xlsx";
        filepath = filepath + filename;

        try {
            File file = new File(filepath);
            workbook = new SXSSFWorkbook(100);
            String sheetName = (record.getIsPay()!=null && 1==record.getIsPay())?"线上缴费车辆列表":"进出车辆列表";
            SXSSFSheet sheetTwo = workbook.createSheet(sheetName);
            Row rowtwo = sheetTwo.createRow(0);
            Cell cell1 = rowtwo.createCell(0);
            Cell cell2 = rowtwo.createCell(1);
            Cell cell3 = rowtwo.createCell(2);
            Cell cell4 = rowtwo.createCell(3);
            Cell cell5 = rowtwo.createCell(4);
            Cell cell6 = rowtwo.createCell(5);

            cell1.setCellValue("车牌");
            cell2.setCellValue("车主电话");
            cell3.setCellValue("进场时间");
            cell4.setCellValue("出场时间");
            cell5.setCellValue("停车时长(分钟)");
            cell6.setCellValue("缴费金额(元)");

            for (int i = 0; i < carPayList.size(); i++) {
                rowtwo = sheetTwo.createRow(i + 1);
                cell1 = rowtwo.createCell(0);
                cell2 = rowtwo.createCell(1);
                cell3 = rowtwo.createCell(2);
                cell4 = rowtwo.createCell(3);
                cell5 = rowtwo.createCell(4);
                cell6 = rowtwo.createCell(5);

                WxCarPayRecord entity = carPayList.get(i);

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                cell1.setCellValue(entity.getPlateNumber());
                cell2.setCellValue(entity.getPhone());
                if(entity.getEntranceTime() != null) {
                    cell3.setCellValue(sdf.format(entity.getEntranceTime()));
                }
                if(entity.getExitTime() != null) {
                    cell4.setCellValue(sdf.format(entity.getExitTime()));
                }
                if(entity.getStayedTime() != null) {
                    cell5.setCellValue(entity.getStayedTime());
                }
                if(entity.getFee() != null) {
                    cell6.setCellValue(entity.getFee());
                }
            }

            FileOutputStream fileOut = new FileOutputStream(file);
            workbook.write(fileOut);
            fileOut.close();
            workbook.close();
            String name = (record.getIsPay()!=null && 1==record.getIsPay())?"线上缴费车辆数据":"进出车辆数据";
            DownFileUtil.downFile(filepath,name+".xlsx",response, request);
            FileUtils.forceDelete(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
