package com.rrtx.services.api.customer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rrtx.fap.core.entity.R;
import com.rrtx.fap.starter.mybatis.plus.entity.QueryRequest;
import com.rrtx.services.api.customer.service.ICustomerService;
import com.rrtx.services.common.controller.BaseController;
import com.rrtx.services.common.utils.CommonUtil;
import com.rrtx.services.db.entity.HeatingF;
import com.rrtx.services.db.entity.HeatingJ;
import com.rrtx.services.db.entity.OrderInfo;
import com.rrtx.services.db.entity.PowerFailureInformationCapture;
import com.rrtx.services.db.service.IHeatingFService;
import com.rrtx.services.db.service.IHeatingJService;
import com.rrtx.services.db.service.IOrderInfoService;
import com.rrtx.services.db.service.IPowerFailureInformationCaptureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequestMapping("api/customer")
public class CustomerController extends BaseController {
    @Autowired
    private ICustomerService iCustomerService;
    @Autowired
    private IHeatingFService iHeatingFService;
    @Autowired
    private IHeatingJService iHeatingJService;
    @Autowired
    private IOrderInfoService iOrderInfoService;
    @Autowired
    private IPowerFailureInformationCaptureService iPowerFailureInformationCaptureService;

    /**
     * 客户查询
     * @param orderType   业务类型
     * @param accountNum        用户编号
     * @param accountName       用户姓名
     * @param request
     * @return
     */
    @RequestMapping("customerList")
    public R getCustomerList(String orderType, String accountNum,String accountName ,QueryRequest request) {
        Map map = new HashMap();
        if(!CommonUtil.isEmpty(orderType)){
            map.put("orderType",orderType);
        }
        if(!CommonUtil.isEmpty(accountName)){
            map.put("accountName",accountName);
        }
        if(!CommonUtil.isEmpty(accountNum)){
            map.put("accountNum",accountNum);
        }
        //客户列表查询
        IPage<Map> list = this.iCustomerService.getCustomer(request,map);
        return R.ok(list);
    }

    /**
     * 用户详情查询
     * @param accountNum
     * @param request
     * @return
     */
    @RequestMapping("queryDetail")
    public R queryDetail( String accountNum ,QueryRequest request) {
        Map map = new HashMap();
        if(!CommonUtil.isEmpty(accountNum)){
            map.put("accountNum",accountNum);
        }
        List<Map> list = this.iCustomerService.getOrderTotal(map);
        //煤改电
        if(!CommonUtil.isEmpty(accountNum)){
            LambdaQueryWrapper<HeatingJ> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(HeatingJ::getHJUserNumber,accountNum);
            int heatingJS = this.iHeatingJService.count(queryWrapper);
            LambdaQueryWrapper<HeatingF> queryWrapperF = new LambdaQueryWrapper();
            queryWrapperF.eq(HeatingF::getHFUserNumber,accountNum);
            int heatingFS = this.iHeatingFService.count(queryWrapperF);
            String num = CommonUtil.add(heatingJS+"",heatingFS+"");
            Map maps = new HashMap();
            maps.put("orderType","煤改电");
            maps.put("numbers",num);
            list.add(maps);
        }
        //客户基本信息
        LambdaQueryWrapper<OrderInfo> infos = new LambdaQueryWrapper<>();
        infos.eq(OrderInfo::getAccountNum,accountNum);
        infos.orderByDesc(OrderInfo::getOrderOperCtx);
        List<OrderInfo>  info = this.iOrderInfoService.findOrderInfos(infos);
        //查询客户电能表停电


        Map dataMap = new HashMap();
        //投诉意见饼状图
        dataMap.put("cakeTotal",list);
        //客户基本信息
        if(info.size()>0){
            dataMap.put("userInfo",info.get(0));
        }else{
            dataMap.put("userInfo","");
        }
        return R.ok(dataMap);
    }

    /**
     * 停电查询
     * @param accountNum  用户编号
     * @param request
     * @return
     */
    @RequestMapping("powerFailure")
    public R getPowerFailure(String accountNum,QueryRequest request) {
        Page<PowerFailureInformationCapture> page = new Page<>(request.getPageNum(), 5);
        LambdaQueryWrapper<PowerFailureInformationCapture> capture = new LambdaQueryWrapper<>();
        capture.eq(PowerFailureInformationCapture::getPoUserNo,accountNum);
        capture.orderByDesc(PowerFailureInformationCapture::getPoEventLogtime);
        IPage<PowerFailureInformationCapture> captureList = this.iPowerFailureInformationCaptureService.page(page,capture);
        //类型转换
        for(PowerFailureInformationCapture capture1:captureList.getRecords()){
            if("1".equals(capture1.getPoPoweroffType())){
                capture1.setPoPoweroffType("计划停电");
            }else if("2".equals(capture1.getPoPoweroffType())){
                capture1.setPoPoweroffType("故障停电");
            }else if("3".equals(capture1.getPoPoweroffType())){
                capture1.setPoPoweroffType("其它");
            }
        }
        //客户列表查询
        //getCustomer(map,page);
//        Map<String, Object> dataTable = getDataTable(list);
        return R.ok(captureList);
    }
    /**
     * 投诉意见分析查询
     * @param accountNum  用户编号
     * @param request
     * @return
     */
    @RequestMapping("opinionAnalysis")
    public R getOpinionAnalysis(String accountNum,QueryRequest request) {
        //客户基本信息
        Page<OrderInfo> page = new Page<>(request.getPageNum(), 5);
        LambdaQueryWrapper<OrderInfo> infos = new LambdaQueryWrapper<>();
        infos.eq(OrderInfo::getAccountNum,accountNum);
        infos.orderByDesc(OrderInfo::getOrderOperCtx);
        IPage<OrderInfo>  info = this.iOrderInfoService.page(page,infos);
        //客户列表查询
        //getCustomer(map,page);
//        Map<String, Object> dataTable = getDataTable(list);
        return R.ok(info);
    }
}
