package com.huiju.estateagents.property.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.center.taUser.mapper.TaUserMapper;
import com.huiju.estateagents.property.common.Constant;
import com.huiju.estateagents.property.dao.*;
import com.huiju.estateagents.property.exception.WisdomException;
import com.huiju.estateagents.property.model.*;
import com.huiju.estateagents.property.service.IBillInvoiceService;
import com.huiju.estateagents.property.service.IBillService;
import com.huiju.estateagents.property.service.IBuildingOwnerInfoService;
import com.huiju.estateagents.property.service.IMessageService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
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.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * <p>
 * 缴费项表 服务实现类
 * </p>
 *
 * @author weiximei
 * @since 2019-02-13
 */
@Service
@Slf4j
public class BillServiceImpl extends ServiceImpl<BillMapper, Bill> implements IBillService {

    /** 金额 /^\d+$/ **/
    private Pattern pattern = Pattern.compile("/(^[1-9]([0-9]+)?(\\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\\.[0-9]([0-9])?$)/");

    /** 缴费项表 **/
    @Autowired
    private BillMapper billMapper;

    /** 缴费单 **/
    @Autowired
    private BillInvoiceMapper billInvoiceMapper;

    @Autowired
    private IBillInvoiceService iBillInvoiceService;

    /** 账单流水表 **/
    @Autowired
    private BillStatementMapper billStatementMapper;

    @Autowired
    private TpBuildingOwnerInfoMapper tpBuildingOwnerInfoMapper;

    @Autowired
    private TaUserMapper taUserMapper;

    @Autowired
    private IBuildingOwnerInfoService iBuildingOwnerInfoService;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private IMessageService iMessageService;

    @Autowired
    private TaUserVerifyMapper taUserVerifyMapper;

    @Autowired
    private TpRoomNoMapper tpRoomNoMapper;

    @Autowired
    private TpBillOrderMapper tpBillOrderMapper;

    @Autowired
    private BillStatementMapper tpBillStatementMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean updateBillNameAndBillExplainAndEndDate(TaUser userElement, Bill bill) {
        ResponseBean responseBean = new ResponseBean();
//        Bill bill = JSONObject.parseObject(parameter, Bill.class);
        if (StringUtils.isEmpty(bill.getBillName())) {
            responseBean.addError("收费项名称不能为空");
            return responseBean;
        }
//        if (StringUtils.isEmpty(bill.getBillExplain())) {
//            responseBean.addError("缴费项说明");
//            return responseBean;
//        }

        if (bill.getStartDate() == null) {
            responseBean.addError("截止时间不能为空");
            return responseBean;
        }
        if (bill.getEndDate() == null) {
            responseBean.addError("截止时间不能为空");
            return responseBean;
        }

        Bill selectBill = billMapper.selectById(bill.getId());
        if (selectBill == null) {
            responseBean.addError("缴费项不存在");
            return responseBean;
        }

        billMapper.updateById(bill);
        responseBean.addSuccess("操作成功");
        return responseBean;
    }

    @Override
    public ResponseBean getByBillId(Integer id) {
        ResponseBean responseBean = new ResponseBean();
        Bill bill = billMapper.selectById(id);
        responseBean.addSuccess(bill);
        return responseBean;
    }
    
    /**
     * 获取收费项列表
     *
     * @param bill
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseBean getBillList(Bill bill, Integer pageNum, Integer pageSize) {
        ResponseBean responseBean = new ResponseBean();
        Page page = new Page(pageNum,pageSize);
        // 分页查询
        List<Bill> list = billMapper.selectListByParams(page,bill);
        Map<String, Object> map = Maps.newHashMap();
        map.put("list",list);
        map.put("total",page.getTotal());
        map.put("pageNum", page.getCurrent());
        map.put("pageSize", page.getSize());
        responseBean.addSuccess(map);
        return responseBean;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean getExcelData(MultipartFile file, TaUser userElement) {
        ResponseBean responseBean = new ResponseBean();
        if (file == null) {
            responseBean.addError("对象不能为空");
            return responseBean;
        }

        responseBean = getExcelData(file, responseBean, userElement.getOrgId());
        List<TempBill> tempBills = (List<TempBill>) responseBean.getData();

        Map<String, Object> map = Maps.newHashMap();
        map.put("list", tempBills);
        map.put("total", tempBills.size());
        responseBean.addSuccess(map);

        return responseBean;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseBean saveExcelData(Integer billId, MultipartFile file, Integer communityId, String billName, LocalDateTime startDate, LocalDateTime endDate, String billStatus, TaUser userElement) {
        ResponseBean responseBean = new ResponseBean();

        List<BillInvoice> records = null;
        if (billId != null) {
            // 查询出这个收费单, 存在的缴费单数据
            QueryWrapper<BillInvoice> billInvoiceQueryWrapper = new QueryWrapper<>();
            billInvoiceQueryWrapper.eq("bill_id", billId);
            billInvoiceQueryWrapper.eq("org_id", userElement.getOrgId());
            records = billInvoiceMapper.selectList(billInvoiceQueryWrapper);
        }

        // 如果没有携带 excel 文件, 并且这个收费组里面也没有收费单, 那么就提示 发送账单失败，需要先上传账单excel
        if (null == file && "0".equals(billStatus) && (null == records ||records.size() == 0)) {
            responseBean.addError("发送账单失败，需要先上传账单excel");
            return responseBean;
        }

        responseBean = getExcelData(file, responseBean, userElement.getOrgId());
        List<TempBill> tempBills = (List<TempBill>) responseBean.getData();

        Double sumDouble = 0.0;
        String strSumDouble = sumDouble + "";
        // 如果 List 有数据, 表示携带了文件上传, 那么就从excel文件里面读取金额
        if (tempBills.size() > 0) {
            sumDouble = tempBills.stream().mapToDouble(TempBill::getPayPrice).sum();
            strSumDouble = sumDouble + "";
        }
        // 如果 List 没有数据, records 有数据, 就表示用户做的修改操作 并且没有上传文件, 那么这里的金额应该从数据库读取
        if (tempBills.size() == 0 && records != null) {
            sumDouble = records.stream().mapToDouble(BillInvoice::getPayPrice).sum();
            strSumDouble = sumDouble + "";
        }

        // 存入缴费项表
        Bill bill = new Bill();
        bill.setCommunityId(communityId);
        bill.setBillName(billName);
//        bill.setBillExplain(billExplain);
        bill.setStartDate(startDate);
        bill.setEndDate(endDate);
        if (StringUtils.isNotBlank(strSumDouble)) {
            strSumDouble = strSumDouble.substring(0, strSumDouble.lastIndexOf("."));
            bill.setPayPrice(strSumDouble);
        }

        bill.setPayedNum(0);
        bill.setBillStatus(billStatus);
        //为草稿时不创建时间, 2表示草稿
        if(!"2".equals(billStatus)) {
            bill.setCreateDate(new Date());
            bill.setCreateUser(userElement.getUserId());
        }
        bill.setOrgId(userElement.getOrgId());
        Integer payTotalNum = tempBills.size() != 0 ? tempBills.size() : records != null ? records.size() : 0;
        Integer unpayedNum = tempBills.size() != 0 ? tempBills.size() : records != null ? records.size() : 0;
        bill.setPayTotalNum(payTotalNum);
        bill.setUnpayedNum(unpayedNum);

        if (file != null) {
            // 删除掉旧数据
            QueryWrapper<BillInvoice> billInvoiceQuery = new QueryWrapper<>();
            billInvoiceQuery.eq("bill_id", billId);
            billInvoiceMapper.delete(billInvoiceQuery);
        }

        if (billId != null) {
            bill.setCreateUser(null);
            bill.setCreateDate(null);
            if(!"2".equals(billStatus)) {
                bill.setCreateDate(new Date());
                bill.setCreateUser(userElement.getUserId());
            }
            // 设置主键, 表示修改
            bill.setId(billId);
        }

        // int rows = billMapper.insert(bill);
        boolean b = this.saveOrUpdate(bill);
        if (b) {
            QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id", userElement.getOrgId());
            queryWrapper.eq("message_type", "7");
            queryWrapper.eq("model_type", "2");
            queryWrapper.eq("source", "2");
            queryWrapper.eq("status", "1");
            queryWrapper.eq("advice_type","4");
            queryWrapper.eq("bill_id", billId);
            Message message = messageMapper.selectOne(queryWrapper);
            // 草稿时插入消息
            if ("2".equals(bill.getBillStatus()) && null == message) {
                message = new Message();
                message.setOrgId(userElement.getOrgId());
                message.setMessageType("7");
                message.setAdviceType("4");
                message.setModelType("2");
                message.setUuid(userElement.getUserId().toString());
                message.setUuidType("2");
                // 消息是物业端发过来的, 就是 2
                message.setSource("2");
                message.setMessageContent("收费组“" + bill.getBillName() +  "”还未发布，点击发布");
                message.setStatus("1");
                message.setCreateDate(LocalDateTime.now());
                message.setCreateUser(userElement.getUserId().toString());
                message.setBillId(bill.getId());

                messageMapper.insert(message);

            }
            // 发布时消息为无效
            if ("0".equals(bill.getBillStatus())) {
                if (null != message) {
                    message.setStatus("0");
                    message.setUpdateDate(LocalDateTime.now());
                    message.setUpdateUser(userElement.getUserId());
                    messageMapper.updateById(message);
                }

                // 插入消息表, 消息类型为 消息
                Message messageModel = new Message();
                messageModel.setOrgId(userElement.getOrgId());
                messageModel.setMessageType("7");
                messageModel.setAdviceType("4");
                messageModel.setModelType("1");
                messageModel.setUuid(userElement.getUserId().toString());
                messageModel.setUuidType("2");
                // 消息是物业端发过来的, 就是 2
                messageModel.setSource("2");

//                messageModel.setMessageContent("收费组“" + bill.getBillName() + "”还有" + DateUtils.getDaysBetween(new Date(), bill.getEndDate()) + "天就到截止日期了，但还有未缴用户，点击去查看");
                messageModel.setStatus("1");
                messageModel.setCreateDate(LocalDateTime.now());
                messageModel.setCreateUser(userElement.getUserId().toString());
                messageModel.setBillId(bill.getId());

                messageMapper.insert(messageModel);


            }
            List<BillInvoice> billInvoices = tempBills.stream().map(e -> {
                BillInvoice billInvoice = new BillInvoice();
                billInvoice.setOrgId(userElement.getOrgId());
                billInvoice.setCommunityId(communityId);
                billInvoice.setBillId(bill.getId());

                /**
                 * 查询房号
                 */
                TpRoomNo tpRoomNo = tpRoomNoMapper.selectCommunityIdAndAddressName(userElement.getOrgId(), e.getPhase(), e.getBuilding(), e.getUnit(), e.getLevel(), e.getRoomNo());
                if (null == tpRoomNo) {
                    throw new WisdomException("房产：" + e.getPhase() + e.getBuilding() + e.getUnit() + e.getLevel() + e.getRoomNo() + " 不存在！请核对！");
                }
                billInvoice.setRoomNoId(tpRoomNo.getId());
                billInvoice.setBillInvoiceExplain(billName);
                billInvoice.setPayPrice(e.getPayPrice());
                billInvoice.setBillStatus("0");
                billInvoice.setCreateUser(userElement.getUserId());
                billInvoice.setCreateDate(new Date());
                billInvoice.setStatus(1);

                return billInvoice;
            }).collect(Collectors.toList());

            boolean batchBool = iBillInvoiceService.saveBatch(billInvoices);
            if (batchBool) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
                // 发布时, 往 APP端推送
                if ("0".equals(bill.getBillStatus())) {
                    List<Message> messageList = billInvoices.stream().map(e -> {

                        // 往APP端用户上面的每一个用户推送
                        Message messageModelAPP = new Message();
                        messageModelAPP.setOrgId(userElement.getOrgId());
                        messageModelAPP.setMessageType("7");
                        messageModelAPP.setAdviceType("1");
                        messageModelAPP.setModelType("2");
                        messageModelAPP.setUuid(null != e.getTaUserId() ? e.getTaUserId().toString() : "");
                        messageModelAPP.setUuidType("1");
                        // 消息是物业端 发过来的, 就是 2
                        messageModelAPP.setSource("2");
                        messageModelAPP.setMessageContent("您有待缴费的账单，点击查看并缴费 " + dateFormat.format(new Date()) + bill.getBillName());
                        messageModelAPP.setStatus("1");
                        messageModelAPP.setCreateDate(LocalDateTime.now());
                        messageModelAPP.setCreateUser(userElement.getUserId().toString());
                        messageModelAPP.setBillId(e.getId());

                        return messageModelAPP;

                    }).collect(Collectors.toList());
//                    iMessageService.saveBatch(messageList);
                }

            }
        }
        responseBean.addSuccess("操作成功!");
        return responseBean;
    }

    private ResponseBean getExcelData(MultipartFile file, ResponseBean responseBean, Integer orgId) {

        // 数据
        List<TempBill> tempBills = Lists.newArrayList();
        if (null == file) {
            responseBean.addSuccess(tempBills);
            return responseBean;
        }

        //获取文件的名字
        String originalFilename = file.getOriginalFilename();
        Workbook workbook = null;
        try {
            if (originalFilename.endsWith(Constant.SUFFIX_2003)) {
                workbook = new HSSFWorkbook(file.getInputStream());
            } else if (originalFilename.endsWith(Constant.SUFFIX_2007)) {
                workbook = new XSSFWorkbook(file.getInputStream());
            }
        } catch (Exception e) {
            log.info(originalFilename);
            e.printStackTrace();
            responseBean.addError("格式错误");
            return responseBean;
        }

        // 获取 Sheet 第一页
        Sheet sheetAt = workbook.getSheetAt(0);
        int lastRowNum = sheetAt.getLastRowNum();

        // 从第7行开始读取
        for (int i= 6; i<= lastRowNum; i++) {
            int currentRow = i + 1;
            Row row = sheetAt.getRow(i);
            Cell cell = getCell(row.getCell(0));
            if (null == cell) {
                throw new WisdomException("第" + currentRow + "行 编号 不能为空");
            }
            // 获取编号
            String roomId = cell.getStringCellValue().trim();
            QueryWrapper<TpRoomNo> roomNoQueryWrapper = new QueryWrapper<>();
            roomNoQueryWrapper.eq("org_id", orgId);
            roomNoQueryWrapper.eq("id", roomId);
            TpRoomNo tpRoomNo = tpRoomNoMapper.selectOne(roomNoQueryWrapper);
            if (null == tpRoomNo) {
                throw new WisdomException("编号: " + roomId + " 不是本小区的房产");
            }
            cell = getCell(row.getCell(1));
            if (null == cell) {
                throw new WisdomException("第" + currentRow + "行 期/区 不能为空");
            }
            String phase = cell.getStringCellValue().trim();
            cell = getCell(row.getCell(2));
            if (null == cell) {
                throw new WisdomException("第" + currentRow + "行 楼栋 不能为空");
            }
            String building = cell.getStringCellValue().trim();
            cell = getCell(row.getCell(3));
            if (null == cell) {
                throw new WisdomException("第" + currentRow + "行 单元 不能为空");
            }
            String unit = cell.getStringCellValue().trim();
            cell = getCell(row.getCell(4));
            if (null == cell) {
                throw new WisdomException("第" + currentRow + "行 楼层 不能为空");
            }
            String level = cell.getStringCellValue().trim();
            cell = getCell(row.getCell(5));
            if (null == cell) {
                throw new WisdomException("第" + currentRow + "行 房号 不能为空");
            }
            String roomNo = cell.getStringCellValue().trim();


//            // 收费说明可以为空
//            cell = getCell(row.getCell(6));
//            String billExplain = null;
//            if (null != cell) {
//                billExplain = cell.getStringCellValue().trim();
//                if (billExplain.length() > 100) {
//                    throw new WisdomException("第" + currentRow + "行 收费说明超出100字，请精简到100字以内");
//                }
//            }

            cell = getCell(row.getCell(6));
            if (null == cell) {
                throw new WisdomException("第" + currentRow + "行 金额 不能为空");
            }
            String money = cell.getStringCellValue().trim();


            TempBill temp = new TempBill();
            temp.setPhase(phase);
            temp.setBuilding(building);
            temp.setLevel(level);
            temp.setUnit(unit);
            temp.setRoomNo(roomNo);
            // temp.setOwnerName(ownerName);
//            temp.setBillExplain(billExplain);


            if (StringUtils.isNotBlank(money)) {
                Double moneyDouble = Double.valueOf(money) * 100;
                money = moneyDouble + "";
                money = money.substring(0, money.lastIndexOf("."));
                temp.setPayPrice(Integer.valueOf(money));
//                Matcher matcher = pattern.matcher(money);
//                if (matcher.find()) {
//                    temp.setMoney(PayPriceUtils.yuanConversionPoints(Double.valueOf(money)*100));
//                } else {
//                    throw new WisdomException("第" + i + "行 金额 格式不合法");
//                }
//                else {
//                    throw new WisdomException("第" + i + "行 金额 格式不合法");
//                }
            }

            tempBills.add(temp);
        }

        responseBean.addSuccess(tempBills);
        return responseBean;
    }

    @Override
    public ResponseBean updateBillStatus(TaUser userElement, Integer billStatus, Integer id) {
        ResponseBean responseBean = new ResponseBean();
        Bill bill = billMapper.selectById(id);
        if (null == bill) {
            responseBean.addError("数据不存在!");
            return responseBean;
        }
        bill.setBillStatus(billStatus + "");
        billMapper.updateById(bill);
        responseBean.addSuccess("操作成功!");
        return responseBean;
    }

    @Override
    public ResponseBean deleteBillBeach(TaUser userElement, List<Integer> ids) {
        ResponseBean responseBean = new ResponseBean();
        ids.forEach(e -> {
            Bill bill = billMapper.selectById(e);
            Integer payedNum = bill.getPayedNum();
            if (payedNum > 0) {
                throw new WisdomException("项目已经有人缴费了，无法删除");
            }
            billMapper.deleteById(bill);
            QueryWrapper<BillInvoice> deleteQueryWrapper = new QueryWrapper<>();
            deleteQueryWrapper.eq("bill_id", bill.getId());
            billInvoiceMapper.delete(deleteQueryWrapper);

            // 发布时消息为无效
            QueryWrapper<Message> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id", userElement.getOrgId());
            queryWrapper.eq("message_type", "7");
            queryWrapper.eq("model_type", "2");
            queryWrapper.eq("source", "2");
            queryWrapper.eq("source", "2");
            queryWrapper.eq("uuid_type", "1");

            List<Message> messages = messageMapper.selectList(queryWrapper);
            messages.forEach(message-> {
                message.setStatus("0");
                message.setUpdateDate(LocalDateTime.now());
                message.setUpdateUser(userElement.getUserId());
                messageMapper.updateById(message);
            });

        });

        responseBean.addSuccess("操作成功!");
        return responseBean;
    }

    private Cell getCell(Cell ce){
        Cell cell = ce;
        if (null == cell) {
            return null;
        }
        //把数字当成String来读，避免出现1读成1.0的情况
        if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
            cell.setCellType(Cell.CELL_TYPE_STRING);
        }

        return cell;
    }

    @Data
    private class TempBill {
        // 期 / 区
        private String phase;
        // 栋
        private String building;
        // 单元
        private String unit;
        // 楼层
        private String level;
        // 户号
        private String roomNo;
        // 业主姓名
        private String ownerName;
        // 金额
        private Integer payPrice;

        // 收费说明
        private String billExplain;
    }

    @Override
    public ResponseBean downloadExcel(TaUser userElement, Integer communityId, String phaseId,String buildingId) {
        ResponseBean responseBean = new ResponseBean();

        InputStream inputStream = null;
        inputStream = this.getClass().getResourceAsStream("/缴费单模板.xls");

        HSSFWorkbook workbook = null;
        try {
            workbook = new HSSFWorkbook(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        HSSFSheet sheet = workbook.getSheetAt(0);
        // 创建行
        HSSFRow row = null;
        // 创建列
        HSSFCell cell = null;

        Integer pageNum = 0;
        Integer pageSize = 10;
        // 分页查询

        List<TpRoomNo> records = tpRoomNoMapper.selectComunityList(userElement.getOrgId(), communityId, phaseId,buildingId,pageNum * pageSize, pageSize);
        // 总页数
        Long pages = tpRoomNoMapper.selectCommunityIdCount(userElement.getOrgId(), communityId);


        // 行数 默认从 第 6 行开始
        int currentRow = 6;
        // 记录坐标
        int index = 0;

        if (records.size() > 0) {
            do {
                TpRoomNo tpRoomNo = records.get(index);
                row = sheet.createRow(currentRow);
                cell = row.createCell(0);
                cell.setCellValue(tpRoomNo.getId() + "");
                cell = row.createCell(1);
                cell.setCellValue(tpRoomNo.getPhaseName());
                cell = row.createCell(2);
                cell.setCellValue(tpRoomNo.getBuildingName());
                cell = row.createCell(3);
                cell.setCellValue(tpRoomNo.getUnitName());
                cell = row.createCell(4);
                cell.setCellValue(tpRoomNo.getLevelName());
                cell = row.createCell(5);
                cell.setCellValue(tpRoomNo.getName());
                currentRow ++;
                index ++;
                if (index == records.size()) {
                    index = 0;
                    pageNum ++;
                    records = tpRoomNoMapper.selectComunityList(userElement.getOrgId(), communityId, phaseId,buildingId, pageNum * pageSize, pageSize);
                }
                if (records.isEmpty()) {
                    break;
                }
            } while (pageNum <= pages);
        }

        responseBean.addSuccess(workbook);
        return responseBean;
    }

    private void setExcelData(String [][] data, HSSFSheet sheet, HSSFRow row, HSSFCell cell){
        int crrentRow = 5;
        // 行数
        for (int i = 0; i < data.length; i++) {
            row = sheet.createRow(i + crrentRow);
            for (int t = 0; t < data[i].length; t++) {
                cell = row.createCell(t);
                cell.setCellValue(data[i][t]);
            }
        }
    }

    /**
     * 根据payType分页获取缴费或未缴费的数据
     * @param personId
     * @param payType
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Map<String, Object> getBillsList(String personId, Integer payType, Integer pageNum, Integer pageSize,Integer taUserVerifyId) {


        /**
         * 账单 关联的是房产,
         */

        //使用分页插件
        IPage<Map<String,Object>> page = new Page<>(pageNum, pageSize);
        IPage<Map<String,Object>> billsPage = billInvoiceMapper.getBillsList(page,1, personId, payType,taUserVerifyId);
        List<Map<String, Object>> billsList = billsPage.getRecords();
        billsList.forEach(e-> {
            String payPrice = String.valueOf(e.get("payPrice"));
            Double payPriceDouble = Double.valueOf(payPrice) / 100;
            e.put("payPrice", payPriceDouble);
        });
        Map<String,Object> result = Maps.newHashMap();
        result.put("list",billsList);
        result.put("total",page.getTotal());
        return result;
    }

    /**
     * 获取账单的详细信息
     * @param billInvoiceId
     * @return
     */
    @Override
    public Map<String, Object> getBillInvoiceDetail(Integer orgId,Integer billInvoiceId) {
        Map<String, Object> billInvoiceDetail = billInvoiceMapper.getBillInvoiceDetail(orgId, billInvoiceId);
        String payPrice = String.valueOf(billInvoiceDetail.get("payPrice"));
        Double payPriceDouble = Double.valueOf(payPrice) / 100;
        billInvoiceDetail.put("payPrice", payPriceDouble);
        return billInvoiceDetail;
    }

    @Override
    public ResponseBean payHistory(TaUser userElement) {
        ResponseBean responseBean = new ResponseBean();

        //1. 查询所有订单
        //2. 根据订单


        // 所有订单
        List<TpBillOrder> billOrderList = tpBillOrderMapper.selectByRoomNoId(userElement.getRoomNoId());
        billOrderList.forEach(e->{
            // 这个订单的所有缴费信息
            List<TpBillOrder> orderList = tpBillOrderMapper.selectByOrderBumber(e.getOrderBumber());

            // 缴费组
            List<BillInvoice> tpBillInvoices = orderList.stream().map(billOrder -> billInvoiceMapper.selectByIdBillInvoiceInfo(billOrder.getTpBillInvoiceId())).collect(Collectors.toList());
            tpBillInvoices.forEach( billInvoice -> billInvoice.setSumPayPrice(Double.valueOf(billInvoice.getPayPrice() + "") / 100));

            // 所有缴费组的总额
            Long billCountPrice = Long.valueOf(tpBillInvoices.stream().map(billCount -> billCount.getPayPrice()).count() + "") / 100;


            e.setBillList(tpBillInvoices);
            e.setSumPrice(billCountPrice);
        });

        responseBean.addSuccess(billOrderList);
        return responseBean;
    }

    @Override
    public ResponseBean getOrderInfo(String orderNumber) {
        ResponseBean responseBean = new ResponseBean();

        /**
         * 1.根据订单号获取所有缴费组
         * 2.获取其中的一条缴费订单数据, 因为订单号再库里面是多条，取第一条就可以
         * 3.计算出所有缴费组的总额
         * 4.获取一个缴费的流水记录, 因为 订单号对应多个缴费单，每一个缴费单对应一个缴费流水, 然后通过缴费流水可以知道缴费人，缴费类型，缴费备注等等
         */

        // 这个订单所有的缴费组记录, 拿到每一个缴费组的id
        List<TpBillOrder> billOrderList = tpBillOrderMapper.selectByOrderNumberBill(orderNumber);
        if (CollectionUtils.isEmpty(billOrderList)) {
            responseBean.addError("订单号不存在！");
            return responseBean;
        }
        // 每个缴费单数据
        List<BillInvoice> tpBillInvoices = billOrderList.stream().map(e -> billInvoiceMapper.selectByIdBillInvoiceInfo(e.getTpBillInvoiceId())).collect(Collectors.toList());
        tpBillInvoices.forEach(e -> e.setSumPayPrice(Double.valueOf(e.getPayPrice() + "") / 100));

        // 这个订单总额
        Double billCountPrice = Double.valueOf(tpBillInvoices.stream().map(e -> e.getPayPrice()).count() + "") / 100;

        // 因为订单号是多条，取第一条即可
        TpBillOrder tpBillOrder = billOrderList.get(0);

        // 获取一条流水
        BillStatement tpBillStatement = tpBillStatementMapper.selectByCommunityIdAndBillInvoiceId(tpBillOrder.getOrgId(), tpBillOrder.getTpBillInvoiceId());


        Map<String, Object> map = Maps.newHashMap();
        map.put("orderNumber", orderNumber);
        map.put("createDate", tpBillOrder.getCreateDate());
        map.put("sumPrice", billCountPrice);
        map.put("payName", tpBillStatement.getPayName());
        map.put("updateDate", tpBillOrder.getUpdateDate());
        map.put("payType", tpBillStatement.getPayType());
        map.put("payRemark", tpBillStatement.getPayRemark());
        map.put("list", tpBillInvoices);

        responseBean.addSuccess(map);
        return responseBean;
    }

    @Override
    public int getBillHouse(Integer orgId, Integer communityId, String startDate, String endDate) {
        return tpBillOrderMapper.getBillHouse(orgId, communityId, startDate, endDate);
    }

    @Override
    public List<Map<String, Object>> getBillMapList(Integer orgId, Integer communityId, String startDate, String endDate) {
        return tpBillOrderMapper.getBillMapList(orgId, communityId, startDate, endDate);
    }

}
