package com.cool.store.controller.webc;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.dao.*;
import com.cool.store.dto.OpenCityDTO;
import com.cool.store.entity.*;
import com.cool.store.job.XxlJobHandler;
import com.cool.store.mapper.HyOpenAreaInfoMapper;
import com.cool.store.request.xfsgFirstOrderListRequest;
import com.cool.store.response.ResponseResult;
import com.cool.store.response.xfsgFirstOderListResponse;
import com.cool.store.service.CoolStoreStartFlowService;
import com.cool.store.service.RegionService;
import com.cool.store.utils.poi.ExcelUtil;
import com.cool.store.vo.RegionPathNameVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Future;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private CoolStoreStartFlowService coolStoreStartFlowService;
    @Resource
    private EnterpriseUserDAO enterpriseUserDAO;

    @Resource
    private EnterpriseUserRoleDao enterpriseUserRoleDao;

    @Resource
    private HyPartnerUserInfoDAO hyPartnerUserInfoDAO;

    @Resource
    private RegionDao regionDao;

    @Resource
    private SysRoleDao sysRoleDao;

    @Resource
    private UserRegionMappingDAO userRegionMappingDAO;

    @Resource
    private RegionService regionService;

    @Resource
    private XxlJobHandler xxlJobHandler;
    @PostMapping("/getFirstOrders")
    public ResponseResult<xfsgFirstOderListResponse> getFirstOrders(@RequestBody xfsgFirstOrderListRequest storeCodeList) {
        xfsgFirstOderListResponse firstOrderList = coolStoreStartFlowService.getFirstOrderList(storeCodeList);
        return ResponseResult.success(firstOrderList);
    }
    @PostMapping("/importCity")
    public ResponseResult<Integer> importCity(MultipartFile file){
        ExcelUtil<OpenCityDTO> util = new ExcelUtil<>(OpenCityDTO.class);
        try {
            List<OpenCityDTO> lineDOList = util.importExcel(file.getInputStream());
            get(lineDOList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.success(0);
    }

    @Resource
    HyOpenAreaInfoMapper hyOpenAreaInfoMapper;

    public Integer get(List<OpenCityDTO> importTask) {
        List<OpenCityDTO> codes = new ArrayList<>();
        try {
            List<OpenCityDTO> acityDTOS = importTask;
            HashMap<Integer, String> first = new HashMap<>(8);
            for (OpenCityDTO acityDTO : acityDTOS){
                Integer code = acityDTO.getCode();
                //如果code后面三位都是0
                int i = code % 10000;
                if (i == 0){
                    //表示是一级菜单
                    acityDTO.setPath("/"+acityDTO.getName()+"/");
                    first.put(code / 10000,acityDTO.getName());
                    HyOpenAreaInfoDO hyOpenAreaInfoDO = new HyOpenAreaInfoDO();
                    hyOpenAreaInfoDO.setAreaPath(acityDTO.getPath());
                    hyOpenAreaInfoDO.setId(acityDTO.getCode().longValue());
                    hyOpenAreaInfoDO.setAreaName(acityDTO.getName());
                    hyOpenAreaInfoDO.setAreaStatus("open");
                    hyOpenAreaInfoMapper.insertSelective(hyOpenAreaInfoDO);
                    continue;
                }
                //二级菜单
                int j = code % 100;
                if (j == 0){
                    //北京市直辖市
                    String name = acityDTO.getName();
                    //code
                    int k = code / 10000;
                    String firstName = first.get(k);
                    if (firstName == null){
                        codes.add(acityDTO);
                        log.info("========================={}",acityDTO);
                        continue;
                    }
                    // 先对name中相同的字段进行去重
                    String newName = name.replace(firstName, "");
                    // 然后拼接path
                    acityDTO.setPath("/"+firstName+"/"+newName+"/");
                    acityDTO.setName(newName);
                    HyOpenAreaInfoDO hyOpenAreaInfoDO = new HyOpenAreaInfoDO();
                    hyOpenAreaInfoDO.setAreaPath(acityDTO.getPath());
                    hyOpenAreaInfoDO.setId(acityDTO.getCode().longValue());
                    hyOpenAreaInfoDO.setAreaName(acityDTO.getName());
                    hyOpenAreaInfoDO.setAreaStatus("open");
                    hyOpenAreaInfoDO.setParentId(k*10000L);
                    hyOpenAreaInfoMapper.insertSelective(hyOpenAreaInfoDO);
                }

            }
            System.out.println(JSONObject.toJSONString(codes));
        } catch (Exception e) {
        }
        return 0;
    }



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

    @GetMapping("/getUserInfoByUserIds")
    public ResponseResult getUserInfoByUserIds(@RequestParam(value = "userIdList", required = false) List<String> userIdList){
        List<EnterpriseUserDO> enterpriseUserDOList = enterpriseUserDAO.getUserInfoByUserIds(userIdList);
        return ResponseResult.success(enterpriseUserDOList);
    }

    @GetMapping("/getUserRoleIds")
    public ResponseResult getUserRoleIds(@RequestParam("userId")String userId){
        List<Long> roleIdList = enterpriseUserRoleDao.getUserRoleIds(userId);
        return ResponseResult.success(roleIdList);
    }

    @GetMapping("/selectByMobile")
    public ResponseResult selectByMobile(@RequestParam("mobile")String mobile){
        HyPartnerUserInfoDO hyPartnerUserInfoDO = hyPartnerUserInfoDAO.selectByMobile(mobile);
        return ResponseResult.success(hyPartnerUserInfoDO);
    }

    @GetMapping("/getRegionById")
    public ResponseResult getRegionById(@RequestParam("regionId")Long regionId){
        RegionDO regionDO = regionDao.getRegionById(regionId);
        return ResponseResult.success(regionDO);
    }

    @GetMapping("/selectRoleByRoleIds")
    public ResponseResult selectRoleByRoleIds(@RequestParam("roleIds")List<Long> roleIds){
        List<SysRoleDO> sysRoleDOList = sysRoleDao.selectRoleByRoleIds(roleIds);
        return ResponseResult.success(sysRoleDOList);
    }

    @GetMapping("/listUserRegionMappingByUserId")
    public ResponseResult listUserRegionMappingByUserId(@RequestParam("userIds")List<String> userIds){
        List<UserRegionMappingDO> userRegionMappingDOList = userRegionMappingDAO.listUserRegionMappingByUserId(userIds);
        return ResponseResult.success(userRegionMappingDOList);
    }

    @GetMapping("/getAllRegionName")
    public ResponseResult getAllRegionName(@RequestParam("regionId")Long regionId){
        RegionPathNameVO regionPathNameVO = regionService.getAllRegionName(regionId);
        return ResponseResult.success(regionPathNameVO);
    }
    @GetMapping("/updateFirstOrder")
    public ResponseResult updateFirstOrder(){
//        xxlJobHandler.updateFirstOrder();
        return ResponseResult.success();
    }
    @GetMapping("updateEntryTime")
    public ResponseResult updateEntryTime(){
        xxlJobHandler.updateEntryTime();
        return ResponseResult.success();
    }
    @GetMapping("/threeAcceptanceBookingMessage")
    public ResponseResult threeAcceptanceBookingMessage(){
        xxlJobHandler.threeAcceptanceBookingMessage();
        return ResponseResult.success();
    }
}
