package com.blueland.sys.area.web;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blueland.annotation.Log;
import com.blueland.bo.AreaEntity;
import com.blueland.bo.AreaExpEntity;
import com.blueland.model.TArea;
import com.blueland.result.R;
import com.blueland.service.IAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.blueland.util.AreaExcelListener;

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

import static com.blueland.constant.CommonConstant.*;
import static com.blueland.constant.ResponseConstant.*;

@Api(value = "area")
@RestController
@RequestMapping(value = "/api/area")
public class AreaRest {
    @Autowired
    private IAreaService areaService;


    @ResponseBody
    @RequestMapping(value="/list", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
   public Page<AreaEntity> getlist(@RequestParam Map<String,Object> queryParams){
        Page<AreaEntity> page = areaService.getList(queryParams,Integer.parseInt(queryParams.get("pageNum").toString()),Integer.parseInt(queryParams.get("pageSize").toString()));
       return page;
   }

    @ResponseBody
    @RequestMapping(value="/getbyid/{id}", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
   public TArea getbyid(@PathVariable String id){
        TArea areaEntity=areaService.getById(id);

        return  areaEntity;
   }

    /**
     * 更新对象
     * @return
     **/
    @Log(busiType = "更新区域数据", opType = UPDATE, type = "web")
    @RequestMapping(value = "updateArea",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
   public R<AreaEntity> updateArea(@RequestBody AreaEntity model, HttpServletRequest request){
        if (ObjectUtil.isEmpty(model.getId())) {
            throw new RuntimeException("主键不能为空");
        }
        areaService.updateArea(model);
        return R.ok(model);
   }

    /**
     * 查询父级菜单
     * @return
     **/
    @RequestMapping(value = "queryArea",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
    @ResponseBody
   public R<List<AreaEntity>> queryArea(){
        List<AreaEntity> list=areaService.queryArea();
        return R.ok(list);
   }

    /**
     * 保存
     * @return
     **/
    @Log(busiType = "新增区域数据", opType = SAVE, type = "web")
    @RequestMapping(value = "insetArea",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
   public R<AreaEntity> insetArea(@RequestBody AreaEntity areaEntity, HttpServletRequest request){
        areaEntity.setCreateTime(new Date());
        areaEntity.setId(areaEntity.getTid());
        areaService.insetArea(areaEntity);
        return R.ok(areaEntity);
   }

    @Log(busiType = "根据id删除数据", opType = DELETE, type = "web")
    @ResponseBody
    @RequestMapping(value="/deleteArea/{id}", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
   public R<Boolean> deleteArea(@PathVariable String id,HttpServletRequest request){
        if (ObjectUtil.isEmpty(id)) {
            throw new RuntimeException("主键不能为空");
        }
        areaService.deleteArea(id);
        return R.ok();
    }
    /**
     * 导入行政区
     * @param file
     * @return
     */
    @Log(busiType = "导入区域数据", opType = EXPORT, type = "web")
    @ApiOperation(value = "/importArea", notes = "导入行政区域信息（excel）")
    @PostMapping("/importArea")
    public R importArea (
            @RequestParam("file") MultipartFile file,
            HttpServletRequest request
    ) {
        String tenantid="admin";
        String msg = RESULT_CODE_SUCCESS_MSG;
        int resultcode = RESULT_CODE_SUCCESS_CODE;
        if (file.isEmpty()) {
//            return new ServiceResponse("文件异常", 401);
            throw new RuntimeException("文件异常");
        }
        try {
            EasyExcel.read(file.getInputStream(), AreaExpEntity.class,
                    new AreaExcelListener(areaService)).sheet().doRead();
        } catch (RuntimeException e) {
            msg = "导入失败！";
            resultcode = RESULT_SERVER_ERR_CODE;
            throw new RuntimeException(e.getMessage());
//            return new ServiceResponse("文件异常，异常的importArea：" + e.getMessage(), 500);
        } catch (Exception e) {
            e.printStackTrace();
            msg = "导入失败！";
            resultcode = RESULT_SERVER_ERR_CODE;
            throw new RuntimeException(e.getMessage());
//            return new ServiceResponse("文件异常", 401);
        }
        return R.ok(resultcode, msg);
    }


}
