package com.zcxk.charge.web.service.impl;

import com.zcxk.charge.api.dto.MonthRevenueDto;
import com.zcxk.charge.api.dto.MonthSellwaterDto;
import com.zcxk.charge.api.vo.MeterCountVo;
import com.zcxk.charge.api.vo.MonthRevenueEntityVo;
import com.zcxk.charge.api.vo.MonthRevenueVo;
import com.zcxk.charge.api.vo.MonthSellwaterVo;
import com.zcxk.charge.core.dao.RevenueMapper;
import com.zcxk.charge.core.entity.MonthRevenueEntity;
import com.zcxk.charge.core.entity.MonthSellwaterEntity;
import com.zcxk.charge.web.service.RevenueService;
import com.zcxk.core.common.util.BeanCopyUtils;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @description
 * @author yjy
 * @data 2021-2-24
 */
@Service
@Log4j2
public class RevenueServiceImpl implements RevenueService{

    @Resource
    private RevenueMapper revenueMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${http_pool.meterRequesturl}")
    private  String meterRequestUrl;

    /**
     * 查询本月售水信息
     * @return
     */
    @Override
    public List<MonthSellwaterVo> selectMonthSell(Integer companyOrgId) {
        MonthSellwaterVo monthSellwaterDto = new MonthSellwaterVo();
        monthSellwaterDto.setCompanyOrgId(companyOrgId);

        LoginUser loginUser = UserUtil.getCurrentUser();
        monthSellwaterDto.setTenantId(loginUser.getTenantId());
        monthSellwaterDto.setProgramItems(loginUser.getProgramItemList());
        monthSellwaterDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        monthSellwaterDto.setPermissonType(loginUser.getPermissonType());

        MonthSellwaterDto dto = new MonthSellwaterDto();
        BeanCopyUtils.copyProperties(monthSellwaterDto,dto, MonthSellwaterDto.class);
        List<MonthSellwaterVo> monthSell=revenueMapper.selectMonthSell(dto);
        List<MonthSellwaterVo> result=new ArrayList<>();
        Double othersUsage=null;
        if(monthSell.size()>4){
            for(int i=0;i<monthSell.size();i++){
                if(i<3){
                    result.add(monthSell.get(i));
                } else{
                    if(monthSell.get(i).getWaterUsage()!=null&&othersUsage!=null){
                        othersUsage+=monthSell.get(i).getWaterUsage();
                    }else if(monthSell.get(i).getWaterUsage()!=null){
                        othersUsage=monthSell.get(i).getWaterUsage();
                    }
                }
            }
            monthSellwaterDto.setPropertyName("其他用水");
            monthSellwaterDto.setWaterUsage(othersUsage);
            result.add(monthSellwaterDto);
        }else{
            result=monthSell;
        }
        return result;
    }
    /**
     * 查询近6个月总售水量
     *
     * @param companyOrgId
     * @return
     */
    @Override
    public  List<MonthSellwaterVo> selectMonthSellTotal(Integer companyOrgId) {
        MonthSellwaterDto monthSellwaterDto=new MonthSellwaterDto();
        monthSellwaterDto.setCompanyOrgId(companyOrgId);

        LoginUser loginUser = UserUtil.getCurrentUser();
        monthSellwaterDto.setTenantId(loginUser.getTenantId());
        monthSellwaterDto.setProgramItems(loginUser.getProgramItemList());
        monthSellwaterDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        monthSellwaterDto.setPermissonType(loginUser.getPermissonType());
        return revenueMapper.selectMonthSellTotal(monthSellwaterDto);
    }

    /**
     * 查询月营收情况
     *
     * @param companyOrgId
     * @return
     */
    @Override
    public  List<MonthRevenueVo> selectMonthRevenue(Integer companyOrgId, Integer months) {
        MonthRevenueDto monthRevenueDto=new MonthRevenueDto();
        monthRevenueDto.setCompanyOrgId(companyOrgId);
        monthRevenueDto.setMonths(months);

        LoginUser loginUser = UserUtil.getCurrentUser();
        monthRevenueDto.setTenantId(loginUser.getTenantId());
        monthRevenueDto.setProgramItems(loginUser.getProgramItemList());
        monthRevenueDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        monthRevenueDto.setPermissonType(loginUser.getPermissonType());
        return revenueMapper.selectMonthRevenue(monthRevenueDto);
    }

    /**
     * 查询本月各分公司的营收情况
     *
     * @return
     */
    @Override
    public  List<MonthRevenueVo> selectCompanyRevenue() {
        MonthRevenueDto monthRevenueDto=new MonthRevenueDto();

        LoginUser loginUser = UserUtil.getCurrentUser();
        monthRevenueDto.setTenantId(loginUser.getTenantId());
        monthRevenueDto.setProgramItems(loginUser.getProgramItemList());
        monthRevenueDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        monthRevenueDto.setPermissonType(loginUser.getPermissonType());
        return revenueMapper.selectCompanyRevenue(monthRevenueDto);
    }


    /**
     * wangbo
     * 查询前N个月的售水数据
     * @param companyOrgId
     * @param months
     * @return
     */

    @Override
    public List<MonthRevenueVo> selectOverviewRevenue(Integer companyOrgId,Integer months) {

        MonthRevenueDto monthRevenueDto=new MonthRevenueDto();
        monthRevenueDto.setCompanyOrgId(companyOrgId);
        monthRevenueDto.setMonths(months);

        LoginUser loginUser = UserUtil.getCurrentUser();
        monthRevenueDto.setTenantId(loginUser.getTenantId());
        monthRevenueDto.setProgramItems(loginUser.getProgramItemList());
        monthRevenueDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        monthRevenueDto.setPermissonType(loginUser.getPermissonType());
        return revenueMapper.selectOverviewRevenue(monthRevenueDto);
    }

    /**
     * wangbo
     * 查询售水信息-按时间段
     * @param companyOrgId
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public List<MonthRevenueVo> selectRevenueByDate(Integer companyOrgId,String startDate,String endDate) {

        MonthRevenueDto monthRevenueDto=new MonthRevenueDto();
        monthRevenueDto.setCompanyOrgId(companyOrgId);
        monthRevenueDto.setStartDate(startDate);
        monthRevenueDto.setEndDate(endDate);

        LoginUser loginUser = UserUtil.getCurrentUser();
        monthRevenueDto.setTenantId(loginUser.getTenantId());
        monthRevenueDto.setProgramItems(loginUser.getProgramItemList());
        monthRevenueDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        monthRevenueDto.setPermissonType(loginUser.getPermissonType());
        return revenueMapper.selectRevenueByDate(monthRevenueDto);
    }

    /**
     * 查询是否当月有营收数据
     * @return
     */
    @Override
    public  Integer isExistThisMonthRevenue(String tenantId) {
        return revenueMapper.isExistThisMonthRevenue(tenantId);
    }
    /**
     * 插入营收数据
     * @return
     */
    @Override
    public Integer insert(MonthRevenueEntity monthRevenueEntity) {
        return revenueMapper.insert(monthRevenueEntity);
    }
    /**
     * 修改营收数据
     * @return
     */
    @Override
    public  Integer update(MonthRevenueEntity monthRevenueEntity) {
        return revenueMapper.update(monthRevenueEntity);
    }

    /**
     * 查询该租户一级公司id
     * @return
     */
    @Override
    public  Integer selectCompanyIdByTenant(String tenantId) {
        return revenueMapper.selectCompanyIdByTenant(tenantId);
    }

    @Override
    public void syncMeterCount() {
        MeterCountVo meterCountDto = restTemplate.getForObject(meterRequestUrl, MeterCountVo.class);
        if(meterCountDto.getStatus()==0){
            for (MonthRevenueEntityVo monthRevenueEntity : meterCountDto.getData()) {
                //查询当月是否有数据 有就更新 没有就插入
                Integer count =revenueMapper.isExistThisMonthRevenue(monthRevenueEntity.getTenantId());
                if(count==0){
                    Calendar thisMonthFirstDateCal = Calendar.getInstance();
                    thisMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1);
                    monthRevenueEntity.setCollectDate(thisMonthFirstDateCal.getTime());
                    monthRevenueEntity.setCompanyOrgId(revenueMapper.selectCompanyIdByTenant(monthRevenueEntity.getTenantId()));

                    MonthRevenueEntity entity = new MonthRevenueEntity();
                    BeanCopyUtils.copyProperties(monthRevenueEntity, entity,MonthRevenueEntity.class);
                    revenueMapper.insert(entity);
                }else{
                    MonthRevenueEntity entity = new MonthRevenueEntity();
                    BeanCopyUtils.copyProperties(monthRevenueEntity, entity,MonthRevenueEntity.class);
                    revenueMapper.update(entity);
                }
            }
        }
    }

    /**
     * 插入售水数据
     * @return
     */
    @Override
    public  Integer insertSellWater(MonthSellwaterEntity monthSellwaterEntity) {
        return revenueMapper.insertSellWater(monthSellwaterEntity);
    }
    /**
     * 修改售水数据
     * @return
     */
    @Override
    public  Integer updateSellWater(MonthSellwaterEntity monthSellwaterEntity) {
        return revenueMapper.updateSellWater(monthSellwaterEntity);
    }
    /**
     * 修改营业数据
     * @return
     */
    @Override
    public  Integer updateRevenue(MonthRevenueEntity monthRevenueEntity) {
        return revenueMapper.updateRevenue(monthRevenueEntity);
    }
    /**
     * 查询月营收情况
     * @param monthRevenueDto 营业信息查询条件
     * @return
     */
    @Override
    public List<MonthRevenueVo> selectList(MonthRevenueDto monthRevenueDto){
        return revenueMapper.selectList(monthRevenueDto);
    }
    /**
     * 查询某月是否有营收数据
     * @return
     */
    @Override
    public  Integer isExistMonthRevenue(Integer companyOrgId,String collectDateString,Integer id) {
        return revenueMapper.isExistMonthRevenue(companyOrgId,collectDateString,id);
    }
}
