package com.jsowell.api.uniapp;

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.core.page.PageResponse;
import com.jsowell.common.enums.ykc.ReturnCodeEnum;
import com.jsowell.common.exception.BusinessException;
import com.jsowell.common.response.RestApiResponse;
import com.jsowell.pile.dto.PileMemberBindingDTO;
import com.jsowell.pile.dto.QueryPersonPileDTO;
import com.jsowell.pile.service.IPileBasicInfoService;
import com.jsowell.pile.service.IPileMemberRelationService;
import com.jsowell.pile.vo.uniapp.OrderVO;
import com.jsowell.pile.vo.uniapp.PersonPileConnectorSumInfoVO;
import com.jsowell.pile.vo.uniapp.PersonPileRealTimeVO;
import com.jsowell.pile.vo.uniapp.PersonalPileInfoVO;
import com.jsowell.service.PileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 个人桩controller
 *
 * @author JS-ZZA
 * @date 2023/2/23 15:36
 */
@Anonymous
@RestController
@RequestMapping("/uniapp/personalPile")
public class PersonPileController extends BaseController {

    @Autowired
    private IPileMemberRelationService pileMemberRelationService;

    @Autowired
    private PileService pileService;

    @Autowired
    private IPileBasicInfoService pileBasicInfoService;

    /**
     * 用户绑定个人桩
     *
     * http://localhost:8080/uniapp/personalPile/pileMemberBinding
     *
     * @param dto
     * @return
     */
    @RequestMapping("/pileMemberBinding")
    public RestApiResponse<?> pileMemberBinding(HttpServletRequest request, @RequestBody PileMemberBindingDTO dto){
        logger.info("绑定个人桩信息 params:{}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response = null;
        try {
            String memberId = getMemberIdByAuthorization(request);
            dto.setMemberId(memberId);
            int i = pileService.pileMemberBinding(dto);
            response = new RestApiResponse<>(i);
        }catch (BusinessException e){
            logger.error("绑定个人桩信息 error,", e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        }catch (Exception exception){
            logger.error("绑定个人桩信息 error,", exception);
            response = new RestApiResponse<>(ReturnCodeEnum.CODE_BINDING_PERSONAL_PILE_ERROR);
        }
        logger.info("绑定个人桩信息  result:{}", response);
        return response;
    }

    /**
     * 个人桩管理员下发给其他用户
     *
     * http://localhost:8080/uniapp/personalPile/adminIssuePile
     *
     * @param request
     * @param dto
     * @return
     */
    @RequestMapping("/adminIssuePile")
    public RestApiResponse<?> adminIssuePile(HttpServletRequest request, @RequestBody PileMemberBindingDTO dto){
        logger.info("桩管理员下发个人桩 params: {}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response = null;
        try {
            String memberId = getMemberIdByAuthorization(request);
            dto.setMemberId(memberId);
            pileService.adminIssuePile(dto);
            response = new RestApiResponse<>();
        }catch (BusinessException e) {
            logger.error("桩管理员下发个人桩 error", e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        }catch (Exception e){
            logger.error("桩管理员下发个人桩 error", e);
            response = new RestApiResponse<>(ReturnCodeEnum.CODE_ADMIN_ISSUE_ERROR);
        }
        logger.info("桩管理员下发个人桩 result: {}", response);
        return response;
    }


    /**
     * 通过memberId查个人桩列表
     *
     * http://localhost:8080/uniapp/personalPile/getPersonalPileList
     *
     * @return
     */
    @GetMapping("/getPersonalPileList")
    public RestApiResponse<?> getPersonalPileList(HttpServletRequest request){
        RestApiResponse<?> response = null;
        try {
            String memberId = getMemberIdByAuthorization(request);
            logger.info("通过memberId查个人桩列表 params: {}", memberId);
            List<PersonalPileInfoVO> list = pileBasicInfoService.getPileInfoByMemberId(memberId);
            response = new RestApiResponse<>(list);
        }catch (Exception e){
            logger.error("通过memberId查个人桩列表异常", e);
            response = new RestApiResponse<>(ReturnCodeEnum.CODE_GET_PERSONAL_PILE_BY_MEMBER_ID_ERROR);
        }
        logger.info("通过memberId查个人桩列表 result:{}", response);
        return response;
    }


    /**
     * 获取枪口实时数据
     *
     * http://localhost:8080/uniapp/personalPile/getConnectorRealTimeInfo
     *
     * @param request
     * @param dto
     * @return
     */
    @PostMapping("/getConnectorRealTimeInfo")
    public RestApiResponse<?> getConnectorRealTimeInfo(HttpServletRequest request, @RequestBody QueryPersonPileDTO dto){
        logger.info("获取个人桩枪口实时数据  params:{}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response = null;
        try {
            String memberId = getMemberIdByAuthorization(request);
            dto.setMemberId(memberId);
            PersonPileRealTimeVO connectorRealTimeInfo = pileService.getConnectorRealTimeInfo(dto);
            response = new RestApiResponse<>(connectorRealTimeInfo);
        }catch (BusinessException e){
            logger.error("获取个人桩枪口实时数据 error", e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        }catch (Exception e){
            logger.error("获取个人桩枪口实时数据 error", e);
            response = new RestApiResponse<>(ReturnCodeEnum.CODE_GET_PERSONAL_PILE_CONNECTOR_INFO_ERROR);
        }
        logger.info("获取个人桩枪口实时数据 result:{}", response);
        return response;
    }


    /**
     * 获取某枪口某段时间内累计信息
     *
     * http://localhost:8080/uniapp/personalPile/getAccumulativeInfo
     *
     * @param dto
     * @return
     */
    @RequestMapping("/getAccumulativeInfo")
    public RestApiResponse<?> getAccumulativeInfo(HttpServletRequest request, @RequestBody QueryPersonPileDTO dto) {
        logger.info("获取某枪口某段时间内累计信息 params:{}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response = null;
        try {
            String memberId = getMemberIdByAuthorization(request);
            dto.setMemberId(memberId);
            PersonPileConnectorSumInfoVO accumulativeInfo = pileService.getAccumulativeInfo(dto);
            response = new RestApiResponse<>(accumulativeInfo);
        } catch (BusinessException e){
            logger.error("获取某枪口某段时间内累计信息 error", e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        }catch (Exception e){
            logger.error("获取某枪口某段时间内累计信息 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("获取某枪口某段时间内累计信息 result:{}", response);
        return response;
    }

    /**
     * 获取充电记录
     *
     * http://localhost:8080/uniapp/personalPile/getChargingRecord
     *
     * @param request
     * @param dto
     * @return
     */
    @RequestMapping("/getChargingRecord")
    public RestApiResponse<?> getChargingRecord(HttpServletRequest request, @RequestBody QueryPersonPileDTO dto){
        logger.info("获取个人桩充电记录  params:{}", JSONObject.toJSONString(dto));
        RestApiResponse<?> response = null;
        try {
            String memberId = getMemberIdByAuthorization(request);
            dto.setMemberId(memberId);
            PageResponse chargingRecord = pileService.getChargingRecord(dto);
            response = new RestApiResponse<>(chargingRecord);
        }catch (BusinessException e){
            logger.error("获取个人桩充电记录 error", e);
            response = new RestApiResponse<>(e.getCode(), e.getMessage());
        }catch (Exception e){
            logger.error("获取个人桩充电记录 error", e);
            response = new RestApiResponse<>(e);
        }
        logger.info("获取个人桩充电记录  result:{}", response);
        return response;
    }
}
