DataCountController.java 8.45 KB
package com.viontech.keliu.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.fastjson.JSON;
import com.viontech.keliu.dao.DataCountDao;
import com.viontech.keliu.vo.MallVo;
import com.viontech.keliu.vo.PersonVo;
import com.viontech.keliu.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: zhuhai
 * Date: 2022-10-31
 * Time: 21:00
 */
@RestController
public class DataCountController {
    @Autowired
    private DataCountDao dataCountDao;
    private static final Logger logger = LoggerFactory.getLogger(DataCountController.class);

    public DataCountController() {
    }

    @GetMapping({"/personGroup"})
    public Object getPersonGroup(@RequestParam("countdate") @DateTimeFormat(pattern = "yyyy-MM-dd") Date countdate, @RequestParam("accountid") Integer accountid) {
        List<ResultVo> resultVos = new ArrayList();

        try {
            List<PersonVo> personVos = this.dataCountDao.get_person(countdate, accountid);
            List<MallVo> mallVos = this.dataCountDao.get_malls(accountid);
            resultVos = this.getPersonList(mallVos, personVos);
        } catch (Exception ex) {
            logger.info("抓拍数据分组出错{}",ex.getMessage());
        }

        HashMap<String, Object> result = new HashMap();
        if (resultVos.size() > 0) {
            List<ResultVo> resultVoList = resultVos.stream().sorted(Comparator.comparing(ResultVo::getCount_person).reversed()).collect(Collectors.toList());
            result.put("msg_code", 200);
            result.put("msg_info", "成功");
            result.put("data", resultVoList);
        } else {
            result.put("msg_code", 506);
            result.put("msg_info", "数据为空");
            result.put("data", resultVos);
        }

        return result;
    }

    @GetMapping({"/personDownload"})
    public void exportExcel(@RequestParam Integer accountid, @RequestParam("countdate") Date countdate, HttpServletResponse response) throws IOException {
        List<PersonVo> personVos = this.dataCountDao.get_person(countdate, accountid);
        List<MallVo> mallVos = this.dataCountDao.get_malls(accountid);
        List<ResultVo> resultVos = this.getPersonList(mallVos, personVos);

        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("抓拍分组-" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now()), "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            ((ExcelWriterBuilder) EasyExcel.write(response.getOutputStream(), ResultVo.class).head(this.titleHead())).autoCloseStream(Boolean.FALSE).sheet("模板").doWrite(resultVos);
        } catch (IOException var9) {
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap(2);
            map.put("status", "failure");
            map.put("message", "下载文件失败" + var9.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }

    }

    public List<ResultVo> getPersonList(List<MallVo> mallVos, List<PersonVo> personVos) {
        List<ResultVo> resultVos = new ArrayList();
        Iterator iterator = mallVos.iterator();
        while(iterator.hasNext()) {
            MallVo mall = (MallVo) iterator.next();
            ResultVo resultVo = new ResultVo();
            resultVo.setMall_name(mall.getName());
            List<PersonVo> tempList = personVos.stream().filter((p) -> p.getMall_id().equals(mall.getId())).collect(Collectors.toList());
            if (tempList.size() == 0) {
                resultVo.setClerk_num("-");
                resultVo.setCount_person("-");
                resultVo.setCustomer_num("-");
                resultVo.setCustomer_adult("-");
                resultVo.setCustomer_child("-");
                resultVo.setCustomer_alone("-");
                resultVo.setCustomer_group("-");
                resultVos.add(resultVo);
            } else {
                resultVo.setCount_person(String.valueOf(tempList.size()));
                Integer clerkNum = (tempList.stream().filter((t) -> t.getPerson_type().equals(1)).collect(Collectors.toList())).size();
                resultVo.setClerk_num(String.valueOf(clerkNum));
                List<PersonVo> customerList = (tempList.stream().filter((t) -> t.getPerson_type().equals(0)).collect(Collectors.toList())).stream().sorted(Comparator.comparing(PersonVo::getCounttime)).collect(Collectors.toList());
                resultVo.setCustomer_num(String.valueOf(customerList.size()));
                Integer customerAdultNum = 0;
                Integer customerChildNum = 0;
                for (PersonVo personVo : customerList) {
                    Integer mood = personVo.getMood();
                    if (mood == null) {
                        continue;
                    }
                    if (mood.equals(108)) {
                        customerAdultNum += 1;
                    } else if (mood.equals(107)) {
                        customerChildNum += 1;
                    }
                }
                resultVo.setCustomer_adult(String.valueOf(customerAdultNum));
                resultVo.setCustomer_child(String.valueOf(customerChildNum));
                int customer_alone = 0;
                int customer_group = 0;
                StringBuilder str = new StringBuilder();
                for (int i = 0; i < customerList.size() - 1; ++i) {
                    PersonVo pvo1 = customerList.get(i);
                    PersonVo pvo2 = customerList.get(i + 1);
                    Long time1 = pvo1.getCounttime().getTime();
                    Long time2 = pvo2.getCounttime().getTime();
                    int seconds = (int) ((time2 - time1) / 1000L);

                    if (i == 0) {
                        str.append(pvo1.getMood());
                    }
                    if (seconds <= 6) {
                        str.append(",").append(pvo2.getMood());
                    } else {
                        str.append("#").append(pvo2.getMood());
                    }
                }
                String[] group = str.toString().split("#");
                for (String s : group) {
                    String[] split = s.split(",");
                    //单独的加一
                    if (split.length == 1) {
                        customer_alone += 1;
                    } else {
                        //分组加一
                        customer_group += 1;
                    }
                }

                resultVo.setCustomer_alone(String.valueOf(customer_alone));
                resultVo.setCustomer_group(String.valueOf(customer_group));
                resultVos.add(resultVo);
            }
        }
        return resultVos;

    }

    private List<List<String>> titleHead() {
        List<List<String>> list = new ArrayList();
        List<String> head0 = new ArrayList();
        head0.add("广场名称");
        List<String> head1 = new ArrayList();
        head1.add("总人数");
        List<String> head2 = new ArrayList();
        head2.add("店员人数");
        List<String> head3 = new ArrayList();
        head3.add("顾客人数");
        List<String> head4 = new ArrayList();
        head4.add("顾客组数(30秒)");
        List<String> head5 = new ArrayList();
        head5.add("顾客组数(60秒)");
        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        list.add(head5);
        return list;
    }
}