package com.dxhy.auth.controller;

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

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.dxhy.auth.entity.ClientEntity;
import com.dxhy.auth.service.ClientBizService;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang
 * @date 2019年3月26日 下午5:54:29
 *
 */
@RestController
@RequestMapping("service")
@Slf4j
public class ServiceController {

    @Resource
    private ClientBizService clientBizService;

    @RequestMapping("/page")
    public ResponseEntity<?> getPage(@RequestBody Map<String, Object> params) {
        PageUtils page = clientBizService.queryPage(params);
        return ResponseEntity.ok(R.ok().put("page", page));
    }

    /**
     * 保存服务
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResponseEntity<?> save(@RequestBody ClientEntity clientEntity) {
        try {
            // 根据登录名获取用户信息
            ClientEntity client = clientBizService.queryCleint(clientEntity.getCode());
            if (client != null) {
                return ResponseEntity.ok(R.error("保存失败，服务编码不能重复！"));
            }
            clientBizService.saveClient(clientEntity);
            return ResponseEntity.ok(R.ok("保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败！"));
        }

    }

    /**
     * 保存服务
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResponseEntity<?> update(@RequestBody ClientEntity clientEntity) {
        try {
            clientBizService.updateClient(clientEntity);
            return ResponseEntity.ok(R.ok("保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败！"));
        }

    }

    /**
     * 保存服务
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseEntity<?> delete(Long clientId) {
        try {
            clientBizService.deleteClient(clientId);
            return ResponseEntity.ok(R.ok("删除成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("删除失败，请稍后重试！"));
        }

    }

    /**
     * 服务信息
     */
    @RequestMapping("/info/{clientId}")
    @ResponseBody
    public ResponseEntity<?> info(@PathVariable("clientId") Long clientId) {
        ClientEntity clientEntity = clientBizService.queryClientEntity(clientId);

        return ResponseEntity.ok(R.ok().put("client", clientEntity));
    }

    @RequestMapping(value = "/modifyClients", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity<?> modifyClients(@RequestBody Map<String, Object> params) {
        try {

            clientBizService.modifyClientServices(params);
            return ResponseEntity.ok(R.ok("保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));
        }
    }

    @RequestMapping(value = "/{id}/client", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<?> getClients(@PathVariable Long id) {
        List<ClientEntity> clientServices = clientBizService.getClientServices(id);
        return ResponseEntity.ok(R.ok().put("data", clientServices));
    }
}
