package com.mvc.controller;

import com.mvc.entity.TAffiliatedunit;
import com.mvc.model.TAffiliatedunitModel;
import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.service.TAffiliatedunitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "关联单位表控制类")
@RequestMapping("/TAffiliatedunitAction")
@Controller
public class TAffiliatedunitAction {

    @Autowired
    private TAffiliatedunitService tAffiliatedunitService;

    @ApiOperation("分页查询关联单位表")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findAllTAffiliatedunit(@RequestBody TAffiliatedunitModel model){
        try {
            Map<String,Object> res= tAffiliatedunitService.findAllTAffiliatedunit(model);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("新增关联单位表")
    @RequestMapping(value = "/saveTAffiliatedunit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> saveTAffiliatedunit(@RequestBody TAffiliatedunitModel model){
        try {
            TAffiliatedunit entity = tAffiliatedunitService.findAffiliatedunitByUnitName(model.getUnitName());
            if(entity!=null){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","["+model.getUnitName()+"]单位名称已经存在");
                return resMap;
            }

            TAffiliatedunitModel creatModel = tAffiliatedunitService.findTAffiliatedunit(model.getCreateUnitCode());
            if(creatModel!=null)
                if(StringUtils.isEmpty(creatModel.getCreateUnitFalg())||!creatModel.getCreateUnitFalg().equals("1")){
                    Map<String,Object> resMap=new ErrorMap();
                    resMap.put("error","没有创建单位的权限");
                    return resMap;
            }

            TAffiliatedunitModel resModel= tAffiliatedunitService.saveTAffiliatedunit(model);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("更新关联单位表")
    @RequestMapping(value = "/updateTAffiliatedunit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateDeviceAcc(@RequestBody TAffiliatedunitModel model){
        try {
            TAffiliatedunit entity = tAffiliatedunitService.findAffiliatedunitByUnitName(model.getUnitName());
            if(entity!=null&&!entity.getUnitCode().equals(model.getUnitCode())){
                Map<String,Object> resMap=new ErrorMap();
                resMap.put("error","["+model.getUnitName()+"]单位名称已经存在");
                return resMap;
            }

            TAffiliatedunitModel resModel= tAffiliatedunitService.updateTAffiliatedunit(model);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("删除关联单位表")
    @RequestMapping(value = "/deleteTAffiliatedunit/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> deleteTAffiliatedunit(@PathVariable("id") int id){
        try {
            int  res= tAffiliatedunitService.deleteTAffiliatedunit(id);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据站代码获取单位对象")
    @RequestMapping(value = "/findTAffiliatedunit/{unitCode}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findTAffiliatedunit(@PathVariable("unitCode") String unitCode){
        try {
            TAffiliatedunitModel res= tAffiliatedunitService.findTAffiliatedunit(unitCode);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("根据创建单位站代码获取可管理的单位列表(运维添加站点时使用)")
    @RequestMapping(value = "/findAffiliatedunitByCreateUnitCode/{createUnitCode}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findAffiliatedunitByCreateUnitCode(@PathVariable("createUnitCode") String createUnitCode){
        try {
            List<TAffiliatedunitModel> res= tAffiliatedunitService.findAffiliatedunitByCreateUnitCode(createUnitCode);
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }
}
