package cn.com.countrygarden.bi.main.controller.dup;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.dao.common.OrganMapper;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.common.Organ;
import cn.com.countrygarden.bi.main.model.dup.DupOrgan;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.dup.DupCommService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by chenweijiang on 2017/9/13.
 */

@RestController()
@RequestMapping("/api/fenghuang/common")
public class CommonController {

    @Autowired
    private DupCommService dupCommService;

    @Autowired
    private AccessTokenService accessTokenService;
    @Autowired
    private OrganMapper organMapper;

    @RequestMapping(value = "/search", produces = "application/json;charset=utf-8",  method = RequestMethod.GET)
    public Object findComm(@RequestParam(value = "isPilot",required = false,defaultValue = "false") Boolean isPilot,
                           @RequestParam(value = "isManager",required = false,defaultValue = "false") Boolean isManager,
            HttpServletRequest request, HttpServletResponse response){
        String accessToken = request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
        List<DupOrgan> result = dupCommService.findDupCommByUserCode(token.getUserCode(),null,isPilot,isManager);
        return Response.response(result);
    }

    @RequestMapping(value = "/searchCase", produces = "application/json;charset=utf-8",  method = RequestMethod.GET)
    public Object findCommCase(@RequestParam(value = "isPilot",required = false,defaultValue = "false") Boolean isPilot,
                           @RequestParam(value = "isManager",required = false,defaultValue = "false") Boolean isManager,
                           HttpServletRequest request, HttpServletResponse response){
        String accessToken = request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
        List<DupOrgan> result = dupCommService.findDupCommByUserCodeCase(token.getUserCode(),null,isPilot,isManager);
        return Response.response(result);
    }


    @RequestMapping(value = "", produces = "application/json;charset=utf-8",  method = RequestMethod.GET)
    public Object listAll(
                           HttpServletRequest request, HttpServletResponse response){
        String accessToken = request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
        List<DupOrgan> result = dupCommService.listAllComm();
        return Response.response(result);
    }
    @RequestMapping(value = "all", produces = "application/json;charset=utf-8",  method = RequestMethod.GET)
    public Object listAllorg(
                           HttpServletRequest request, HttpServletResponse response){
        String accessToken = request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
        List<Organ> result = organMapper.selectAll();
        return Response.response(result);
    }
    @RequestMapping(value = "", produces = "application/json;charset=utf-8",  method = RequestMethod.POST)
    public Object addManagerComm(
            @RequestParam("commId") String commId,
            HttpServletRequest request, HttpServletResponse response){
        String accessToken = request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
        if(StringUtils.isEmpty(commId)){
            return Response.responseFail("请选择要添加的项目");
        }
        dupCommService.addManagerComm(commId,token.getUserCode());
        return Response.response("");
    }

//    @RequestMapping(value = "/{commId}", produces = "application/json;charset=utf-8",  method = RequestMethod.DELETE)
//    public Object deleteManagerComm(
//            @PathVariable("commId") String commId,
//            HttpServletRequest request, HttpServletResponse response){
//        String accessToken = request.getParameter("AccessToken");
//        AccessToken token = accessTokenService.findByToken(accessToken);
//        if(StringUtils.isEmpty(commId)){
//            return Response.responseFail("请选择移除的项目");
//        }
//        dupCommService.deleteManagerComm(commId);
//        return Response.response("");
//    }
}
