package com.donglan.ahxf.ahxfmeetregisterwindow.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Snowflake;
import com.donglan.ahxf.ahxfmeetregisterwindow.common.Result;
import com.donglan.ahxf.ahxfmeetregisterwindow.entity.Dict;
import com.donglan.ahxf.ahxfmeetregisterwindow.entity.RegisterWindow;
import com.donglan.ahxf.ahxfmeetregisterwindow.service.DictService;
import com.donglan.ahxf.ahxfmeetregisterwindow.service.RegisterWindowService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author shj
 * @date 2020-01-03 13:04
 */
@Controller
@RequestMapping(value = "/registerWindow")
public class RegisterWindowController {

    @Autowired
    private RegisterWindowService registerWindowService;

    @Autowired
    private DictService dictService;

    @Autowired
    private Snowflake snowflake;

    @RequestMapping(value = "/{winNum}",method = RequestMethod.GET)
    @ResponseBody
    @CrossOrigin
    public Result findByWinNum(@PathVariable Integer winNum){
        RegisterWindow registerWindow = registerWindowService.findByWinNum(winNum);
        Map<String, Object> map = BeanUtil.beanToMap(registerWindow);
        map.put("province",registerWindow.getFirstAreaName());
        map.put("city",registerWindow.getSecondAreaName());
        map.put("area",registerWindow.getThirdAreaName());
//        if(registerWindow.getFirstAreaCode()!=null && !StringUtils.isEmpty(registerWindow.getFirstAreaCode())){
//            Dict province = dictService.findByIdAndValueAndIsDelete("AB", registerWindow.getFirstAreaCode(), 1);
//            map.put("province",province.getText());
//        }
//        if(registerWindow.getSecondAreaCode()!=null && !StringUtils.isEmpty(registerWindow.getSecondAreaCode())){
//            Dict city = dictService.findByIdAndValueAndIsDelete("AB", registerWindow.getSecondAreaCode(), 1);
//            map.put("city",city.getText());
//        }
//        if(registerWindow.getThirdAreaCode()!=null && !StringUtils.isEmpty(registerWindow.getThirdAreaCode())){
//            Dict area = dictService.findByIdAndValueAndIsDelete("AB", registerWindow.getThirdAreaCode(), 1);
//            map.put("area",area.getText());
//        }
        return Result.success(map);
    }

    @RequestMapping(value = "",method = RequestMethod.GET)
    @ResponseBody
    @CrossOrigin
    public Result findList(){
        List<RegisterWindow> registerWindowList = registerWindowService.findAll();
        List<Map<String,Object>> windowMapList = new ArrayList<>();
        for(RegisterWindow registerWindow:registerWindowList){
            Map<String, Object> map = BeanUtil.beanToMap(registerWindow);
            map.put("province",registerWindow.getFirstAreaName());
            map.put("city",registerWindow.getSecondAreaName());
            map.put("area",registerWindow.getThirdAreaName());

//            if(registerWindow.getFirstAreaCode()!=null && !StringUtils.isEmpty(registerWindow.getFirstAreaCode())){
//                Dict province = dictService.findByIdAndValueAndIsDelete("AB", registerWindow.getFirstAreaCode(), 1);
//                map.put("province",province.getText());
//            }
//            if(registerWindow.getSecondAreaCode()!=null && !StringUtils.isEmpty(registerWindow.getSecondAreaCode())){
//                Dict city = dictService.findByIdAndValueAndIsDelete("AB", registerWindow.getSecondAreaCode(), 1);
//                map.put("city",city.getText());
//            }
//            if(registerWindow.getThirdAreaCode()!=null && !StringUtils.isEmpty(registerWindow.getThirdAreaCode())){
//                Dict area = dictService.findByIdAndValueAndIsDelete("AB", registerWindow.getThirdAreaCode(), 1);
//                map.put("area",area.getText());
//            }
            windowMapList.add(map);
        }
        return Result.success(windowMapList);
    }

//    @RequestMapping(value = "/goSettingPage")
//    public String goRegisterWindowSetting(Model model){
//        RegisterWindow registerWindow1 = registerWindowService.findByWinNum(1);
//        model.addAttribute("win1",registerWindow1);
//        RegisterWindow registerWindow2 = registerWindowService.findByWinNum(2);
//        model.addAttribute("win2",registerWindow2);
//        List<Dict> areaList = dictService.findAllByIdAndParentValueIsNull("AB");
//        model.addAttribute("areaList",areaList);
//        return "registerWindow";
//    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody RegisterWindow registerWindow){
        if(registerWindow.getId()==null)
            registerWindow.setId(snowflake.nextIdStr());
        if(registerWindow.getFirstAreaCode()!=null && !StringUtils.isEmpty(registerWindow.getFirstAreaCode())){
            Dict province = dictService.findByIdAndValueAndIsDelete("AB", registerWindow.getFirstAreaCode(), 1);
            registerWindow.setFirstAreaName(province.getText());
        }
        if(registerWindow.getSecondAreaCode()!=null && !StringUtils.isEmpty(registerWindow.getSecondAreaCode())){
            Dict city = dictService.findByIdAndValueAndIsDelete("AB", registerWindow.getSecondAreaCode(), 1);
            registerWindow.setSecondAreaName(city.getText());
        }
        if(registerWindow.getThirdAreaCode()!=null && !StringUtils.isEmpty(registerWindow.getThirdAreaCode())){
            Dict area = dictService.findByIdAndValueAndIsDelete("AB", registerWindow.getThirdAreaCode(), 1);
            registerWindow.setThirdAreaName(area.getText());
        }
        registerWindowService.save(registerWindow);
        return Result.success();
    }

    @RequestMapping(value = "/userId",method = RequestMethod.PATCH)
    @ResponseBody
    @CrossOrigin
    public Result updateByUserId(@RequestBody RegisterWindow registerWindow){
        RegisterWindow data = registerWindowService.findByUserId(registerWindow.getUserId());
        BeanUtil.copyProperties(registerWindow,data, CopyOptions.create(null,true,null));
        registerWindowService.save(data);
        return Result.success();
    }
}
