package com.iformall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxBillProperty;
import com.iformall.domain.po.WxPropertyContract;
import com.iformall.domain.po.WxRentContract;
import com.iformall.domain.vo.WxRentPropertyContractVo;
import com.iformall.exception.MallinkException;
import com.iformall.mapper.WxPropertyContractMapper;
import com.iformall.mapper.WxRentContractMapper;
import com.iformall.service.WxPropertyContractService;
import com.iformall.service.WxRentContractService;
import com.iformall.service.WxRentPropertyContractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gongbiao
 */
@Service
public class WxRentPropertyContractServiceImpl implements WxRentPropertyContractService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxRentContractMapper wxRentContractMapper;

    @Autowired
    WxPropertyContractMapper wxPropertyContractMapper;

    @Autowired
    WxPropertyContractService wxPropertyContractService;

    @Autowired
    WxRentContractService wxRentContractService;

    @Override
    public PageInfo<WxRentPropertyContractVo> listContractVo(WxRentPropertyContractVo record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxRentContractMapper.listContractVo(record));
    }

    @Override
    public ResultData getContractInfo(Long id) {
        Map<String, Object> data = new HashMap<>(2);
        Map<String, Object> rent = wxRentContractService.getById(id);
        data.put("rent", rent);
        WxPropertyContract wxPropertyContractQuery = new WxPropertyContract();
        wxPropertyContractQuery.setRentContractId(id);
        WxPropertyContract propertyContract = wxPropertyContractMapper.selectOne(new QueryWrapper(wxPropertyContractQuery));
        if (propertyContract != null) {
            List<WxBillProperty> wxBillPropertyPreview = wxPropertyContractService.getWxBillPropertyPreview(propertyContract.getId());
            propertyContract.setPreviewBillRentList(wxBillPropertyPreview);
            data.put("property", propertyContract);
        }
        return new ResultData(data);
    }


    @Override
    public ResultData getContractApplyInfo(Long id) {
        Map<String, Object> data = new HashMap<>(2);
        Map<String, Object> rent = wxRentContractService.getById(id);
        data.put("rent", rent);
        WxPropertyContract wxPropertyContractQuery = new WxPropertyContract();
        wxPropertyContractQuery.setRentContractId(id);
        WxPropertyContract propertyContract = wxPropertyContractMapper.selectOne(new QueryWrapper(wxPropertyContractQuery));
        if (propertyContract != null) {
            try {
                Map<String, Object> property = wxPropertyContractService.getById(propertyContract.getId());
                data.put("property", property);
            } catch (MallinkException e) {
                logger.error(e.getMessage());
            }
        }
        return new ResultData(data);
    }


    @Override
    public ResultData insertRentForCreate(WxRentContract wxRentContract) {
        return null;
    }
}






