package io.renren.modules.app.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.renren.modules.app.dao.ZhmxzDao;
import io.renren.modules.app.entity.FhmxzEntity;
import io.renren.modules.app.service.ZhmxzService;

@Service("zhmxzService")
public class ZhmxzServiceImpl extends ServiceImpl<ZhmxzDao, FhmxzEntity> implements ZhmxzService {

    public String ye(Integer pageNum, Integer pageSize, FhmxzEntity fhmxzEntity) {
        int limit = (pageNum - 1) * pageSize;
        String qmye = null;
        String dhbm = fhmxzEntity.getDhbm();
        String begdate = fhmxzEntity.getBegdate();
        String enddate = fhmxzEntity.getEnddate();
        // 余额
        double ye = 0.0;
        // 借方本金
        double jfbj = 0.0;
        // 借方利息
        double jflx = 0.0;
        // 贷方本金
        double dfbj = 0.0;
        // 贷方利息
        double dflx = 0.0;
        List<FhmxzEntity> list = baseMapper.selectZhMXZ_2(limit, begdate, enddate, dhbm);
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                ye = Double.parseDouble(list.get(i).getYe());
                continue;
            }
            jfbj = Double.parseDouble(list.get(i).getJfbj());
            jflx = Double.parseDouble(list.get(i).getJflx());
            dfbj = Double.parseDouble(list.get(i).getDfbj());
            dflx = Double.parseDouble(list.get(i).getDflx());
            // 本月余额 = 上月期末余额-贷方本金-贷方利息+借方本金+借方利息
            ye = ye - jfbj - jflx + dfbj + dflx;
            // 保留两位小数四舍五入
            BigDecimal b = new BigDecimal(ye);
            double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

            DecimalFormat df = new DecimalFormat("0.00");// 格式化
            qmye = df.format(f1);
        }
        return qmye;
    }

    /**
     * 幢号明细账 * @param pageNum
     *
     * @param pageSize
     * @param
     * @return
     */
    @Override
    public PageInfo<FhmxzEntity> selectZhMXZByPage(Integer pageNum, Integer pageSize, FhmxzEntity fhmxzEntity) {
		int flag = 0;
        Double jfbj2 = 0.0;
        Double jflx2 = 0.0;
        Double dfbj2 = 0.0;
        Double dflx2 = 0.0;
		// 余额
		double ye = 0.0;
		// 借方本金
		double jfbj = 0.0;
		// 借方利息
		double jflx = 0.0;
		// 贷方本金
		double dfbj = 0.0;
		// 贷方利息
		double dflx = 0.0;
		//如果不是第一页调到计算余额方法
		if (pageNum!=1){
			flag = 1;
			String ljye = ye(pageNum, pageSize, fhmxzEntity);
			ye = Double.parseDouble(ljye);
		}
        List<FhmxzEntity> list2 = baseMapper.selectZhMXZ(fhmxzEntity);
        FhmxzEntity entity = new FhmxzEntity();
        for (int i = 0; i < list2.size(); i++) {
            double jfbj3 = Double.parseDouble(list2.get(i).getJfbj());
            jfbj2 = jfbj2 + jfbj3;

            double jflx3 = Double.parseDouble(list2.get(i).getJflx());
            jflx2 = jflx2 + jflx3;

            double dfbj3 = Double.parseDouble(list2.get(i).getDfbj());
            dfbj2 = dfbj2 + dfbj3;

            double dflx3 = Double.parseDouble(list2.get(i).getDflx());
            dflx2 = dflx2 + dflx3;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<FhmxzEntity> list = baseMapper.selectZhMXZ(fhmxzEntity);

        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
				if (flag != 1) {
					ye = Double.parseDouble(list.get(i).getYe());
					continue;
				}
            }
            jfbj = Double.parseDouble(list.get(i).getJfbj());
            jflx = Double.parseDouble(list.get(i).getJflx());
            dfbj = Double.parseDouble(list.get(i).getDfbj());
            dflx = Double.parseDouble(list.get(i).getDflx());
            // 本月余额 = 上月期末余额-贷方本金-贷方利息+借方本金+借方利息
            ye = ye - jfbj - jflx + dfbj + dflx;
            // 保留两位小数四舍五入
            BigDecimal b = new BigDecimal(ye);
            double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

            DecimalFormat df = new DecimalFormat("0.00");// 格式化
            String qmye = df.format(f1);
            String jfbj1 = df.format(jfbj);
            String jflx1 = df.format(jflx);
            String dfbj1 = df.format(dfbj);
            String dflx1 = df.format(dflx);
            list.get(i).setJfbj(jfbj1);
            list.get(i).setDfbj(dfbj1);
            list.get(i).setJflx(jflx1);
            list.get(i).setDflx(dflx1);
            list.get(i).setYe(qmye);
        }
        entity.setIshj("1");
        entity.setDfbj(dfbj2.toString());
        entity.setDflx(dflx2.toString());
        entity.setJfbj(jfbj2.toString());
        entity.setJflx(jflx2.toString());
        list.add(entity);
        PageInfo<FhmxzEntity> pageInfo = new PageInfo<FhmxzEntity>(list);
        return pageInfo;
    }

}
