package com.cool.store.controller;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.dao.EnterpriseUserDAO;
import com.cool.store.dao.HyPartnerTaskInfoLogDAO;
import com.cool.store.dto.calendar.CreateCalendarEventDTO;
import com.cool.store.dto.calendar.DeleteCalendarEventDTO;
import com.cool.store.dto.calendar.UpdateCalendarEventDTO;
import com.cool.store.dto.calendar.UserCalendarsEventDTO;
import com.cool.store.dto.log.LineLogInfo;
import com.cool.store.dto.message.SendCardMessageDTO;
import com.cool.store.dto.response.ResultDTO;
import com.cool.store.entity.HyOpenAreaInfoDO;

import com.cool.store.entity.EnterpriseUserDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.IDCardSideEnum;
import com.cool.store.exception.ApiException;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.HyOpenAreaInfoMapper;
import com.cool.store.http.ISVHttpRequest;
import com.cool.store.request.City;
import com.cool.store.request.TestRequest;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.*;
import com.cool.store.vo.cuser.IdentityCardInfoVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author zhangchenbiao
 * @FileName: TestController
 * @Description:
 * @date 2023-05-26 11:32
 */
@Slf4j
@RestController
@RequestMapping("test/")
public class TestController {

    @Resource
    private EnterpriseUserService enterpriseUserService;
    @Resource
    private ISVHttpRequest isvHttpRequest;
    @Resource
    private HyOpenAreaInfoMapper hyOpenAreaInfoMapper;
    @Resource
    private EnterpriseSyncService enterpriseSyncService;
    @Resource
    private EnterpriseUserDAO enterpriseUserDAO;
    @Resource
    private AliyunService aliyunService;
    @Resource
    private HyPartnerTaskInfoLogDAO hyPartnerTaskInfoLogDAO;
    @Resource
    private OpenAreaService openAreaService;

    @PostMapping("/post")
    public ResponseResult<Boolean> get(@RequestBody List<TestRequest> testRequestList){
        log.info(JSONObject.toJSONString(testRequestList));
        for (TestRequest testRequest:testRequestList) {
            //省市区
            String name = testRequest.getName();
            HyOpenAreaInfoDO hyOpenAreaInfoDO = new HyOpenAreaInfoDO();
            String replace = name.replace("  ", "");
            hyOpenAreaInfoDO.setAreaName(replace);
            String regionPath = "/"+replace+"/";
            hyOpenAreaInfoDO.setAreaPath(regionPath);
            hyOpenAreaInfoDO.setAreaStatus("open");
            hyOpenAreaInfoMapper.insertSelective(hyOpenAreaInfoDO);
            Long id = hyOpenAreaInfoDO.getId();
            log.info("yuyyyyyyyyyyyyyyyyyyyyy:{}",JSONObject.toJSONString(hyOpenAreaInfoDO));
            List<City> city = testRequest.getCity();
            for (City c:city) {
                String regionPath2 = "";
                Long id1 = 0L;
                if (c.getName().equals("其他")){
                    continue;
                }
                if (c.getName().equals(replace)){
                    //直辖市
                    regionPath2 = regionPath;
                    id1 = id;
                }else {
                    regionPath2 = regionPath +c.getName().replace("  ","")+"/";
                    hyOpenAreaInfoDO = new HyOpenAreaInfoDO();
                    hyOpenAreaInfoDO.setAreaName(c.getName().replace("  ",""));
                    hyOpenAreaInfoDO.setAreaStatus("open");
                    hyOpenAreaInfoDO.setAreaPath(regionPath2);
                    hyOpenAreaInfoDO.setParentId(id);
                    hyOpenAreaInfoMapper.insertSelective(hyOpenAreaInfoDO);
                    log.info("33333333333333333333333333333333:{}",JSONObject.toJSONString(hyOpenAreaInfoDO));
                    id1 = hyOpenAreaInfoDO.getId();
                }

                List<String> area = c.getArea();
                for (String c1:area) {
                    if (c1.equals("其他")){
                        continue;
                    }
                    String regionPath1 = regionPath2 +c1.replace("  ","")+"/";
                    hyOpenAreaInfoDO = new HyOpenAreaInfoDO();
                    hyOpenAreaInfoDO.setAreaName(c1.replace("  ",""));
                    hyOpenAreaInfoDO.setAreaStatus("open");
                    hyOpenAreaInfoDO.setAreaPath(regionPath1);
                    hyOpenAreaInfoDO.setParentId(id1);
                    log.info("hhhhhhhhhhhhhhhhhhhhhhhhhhhh:{}",JSONObject.toJSONString(hyOpenAreaInfoDO));
                    hyOpenAreaInfoMapper.insertSelective(hyOpenAreaInfoDO);
                }
            }
        }
        return ResponseResult.success();
    }

    @GetMapping("getUserInfo")
    public ResponseResult getUserInfo(@RequestParam("userId")String userId){
        EnterpriseUserDO enterpriseUser = enterpriseUserService.getUserInfoByUserId(userId);
        return ResponseResult.success(enterpriseUser);
    }

    @GetMapping("getUserIdByCode")
    public ResponseResult getUserIdByCode(@RequestParam("paramStr")String paramStr){
        return ResponseResult.success(isvHttpRequest.getUserIdByCode(paramStr));
    }

    @GetMapping("getUserDetailByUserId")
    public ResponseResult getUserDetailByUserId(@RequestParam("userId")String userId){
        return ResponseResult.success(isvHttpRequest.getUserDetailByUserId(userId));
    }

    @GetMapping("getAdminUserList")
    public ResponseResult getAdminUserList(){
        return ResponseResult.success(isvHttpRequest.getAdminUserList());
    }

    @GetMapping("getAuthInfo")
    public ResponseResult getAuthInfo(){
        return ResponseResult.success(isvHttpRequest.getAuthInfo());
    }

    @GetMapping("getDepartments")
    public ResponseResult getDepartments(@RequestParam("parentId")String parentId){
        return ResponseResult.success(isvHttpRequest.getDepartments(parentId));
    }

    @GetMapping("getAuthScope")
    public ResponseResult getAuthScope(){
        return ResponseResult.success(isvHttpRequest.getAuthScope());
    }

    @GetMapping("getDepartmentUsers")
    public ResponseResult getDepartmentUsers(@RequestParam("deptId")String deptId){
        return ResponseResult.success(isvHttpRequest.getDepartmentUsers(deptId));
    }

    @GetMapping("getUserDetailByUserIds")
    public ResponseResult getUserDetailByUserIds(@RequestParam("userIds") List<String> userIds){
        return ResponseResult.success(isvHttpRequest.getUserDetailByUserIds(userIds));
    }

    @GetMapping("getSubDepartments")
    public ResponseResult getSubDepartments(@RequestParam("parentId")String parentId, @RequestParam("fetchChild")Boolean fetchChild){
        return ResponseResult.success(isvHttpRequest.getSubDepartments(parentId, fetchChild));
    }

    @GetMapping("syncAll")
    public ResponseResult syncAll(){
        enterpriseSyncService.syncAll();
        return ResponseResult.success();
    }

    @GetMapping("/user/getFreeBusyList")
    public ResultDTO<List<UserCalendarsEventDTO>> getFreeBusyList(@RequestParam("userId") String userId, @RequestParam("startTime") long startTime,
                                                               @RequestParam("endTime") long endTime) throws ApiException {
        log.info("getUserCalendarsEvents : corpId:{}, appType:{}, userId:{}, startTime:{}, endTime:{}", userId, startTime, endTime);
        return ResultDTO.successResult(isvHttpRequest.getFreeBusyList(userId, startTime, endTime));
    }

    @PostMapping("/user/createUserCalendarEvent")
    public ResultDTO<UserCalendarsEventDTO> createUserCalendarEvent(@RequestBody CreateCalendarEventDTO param) throws ApiException {
        log.info("createUserCalendarEvent , param:{}", JSONObject.toJSONString(param));
        return ResultDTO.successResult(isvHttpRequest.createUserCalendarEvent(param));
    }

    @PostMapping("/user/updateUserCalendarEvent")
    public ResultDTO<UserCalendarsEventDTO> updateUserCalendarEvent(@RequestBody UpdateCalendarEventDTO param) throws ApiException {
        log.info("updateUserCalendarEvent , param:{}", JSONObject.toJSONString(param));
        return ResultDTO.successResult(isvHttpRequest.updateUserCalendarEvent(param));
    }

    @PostMapping("/user/deleteUserCalendarEvent")
    public ResultDTO<UserCalendarsEventDTO> deleteUserCalendarEvent(@RequestBody DeleteCalendarEventDTO param) throws ApiException {
        log.info("createUserCalendarEvent , param:{}", JSONObject.toJSONString(param));
        return ResultDTO.successResult(isvHttpRequest.deleteUserCalendarEvent(param));
    }

    @GetMapping("/user/getUserCalendarsEvents")
    public ResultDTO<List<UserCalendarsEventDTO>> getUserCalendarsEvents(@RequestParam("userId") String userId, @RequestParam("startTime") long startTime,
                                                                  @RequestParam("endTime") long endTime) throws ApiException {
        log.info("getUserCalendarsEvents : corpId:{}, appType:{}, userId:{}, startTime:{}, endTime:{}", userId, startTime, endTime);
        return ResultDTO.successResult(isvHttpRequest.getUserCalendarsEvents(userId, startTime, endTime));
    }

    @GetMapping("/getIdentityCardInfo")
    @ApiOperation("根据身份证正面解析获取数据")
    public ResponseResult<IdentityCardInfoVO> getIdentityCardInfo(@RequestParam(value = "faceImageUrl")String faceImageUrl){
        try {
            IdentityCardInfoVO identityCardInfo = aliyunService.getIdentityCardInfo(faceImageUrl, IDCardSideEnum.FACE);
            return ResponseResult.success(identityCardInfo);
        } catch (Exception e) {
            throw new ServiceException(ErrorCodeEnum.IDENTITY_CARD_PARSE_FAIL);
        }
    }

    @PostMapping("/sendFeiShuCardMessage")
    public ResponseResult sendFeiShuCardMessage(@RequestBody SendCardMessageDTO param){
        try {
            isvHttpRequest.sendFeiShuCardMessage(param);
            return ResponseResult.success();
        } catch (Exception e) {
            throw new ServiceException(ErrorCodeEnum.IDENTITY_CARD_PARSE_FAIL);
        }
    }

    @GetMapping("getUserIdsByScope")
    public ResponseResult getUserIdsByScope(@RequestParam("userId") String userId){
        return ResponseResult.success(enterpriseUserService.getUserIdsByScope(userId));
    }

    @GetMapping("getDevelopmentDirectorList")
    public ResponseResult getDevelopmentDirectorList(@RequestParam("wantShopArea") String wantShopArea){
        return ResponseResult.success(enterpriseUserService.getDevelopmentDirectorList(wantShopArea));
    }

    @GetMapping("getDevelopmentByZoneId")
    public ResponseResult getDevelopmentByZoneId(@RequestParam("zoneId") Long zoneId){
        return ResponseResult.success(enterpriseUserService.getDevelopmentByZoneId(zoneId));
    }

    @GetMapping("/searchOpenArea")
    public ResponseResult searchOpenArea(@RequestParam(value = "keyword",required = false)String keyword, @RequestParam(value = "applyFlag",required = false)Boolean applyFlag){
        return ResponseResult.success(openAreaService.searchOpenArea(keyword, applyFlag, Boolean.FALSE));
    }

    @Resource
    HyPhoneLocationService hyPhoneLocationService;

    @GetMapping("/handleHyPhoneAddress")
    public ResponseResult test(@RequestParam(value = "phone",required = false)String phone){
        hyPhoneLocationService.handleHyPhoneAddress(phone);
        return ResponseResult.success(Boolean.TRUE);
    }

    @GetMapping("/getWechatAccessToken")
    public ResponseResult getWechatAccessToken(@RequestParam(value = "appId")String appId, @RequestParam("appSecret")String appSecret){
        try {
            return ResponseResult.success(isvHttpRequest.getWechatAccessToken(appId, appSecret));
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return null;
    }
}
