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

import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.dto.districtReport.RdDistrictInfoDTO;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.districtReport.RdDistrictPrincipal;
import cn.com.countrygarden.bi.main.model.dup.DupOrgan;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.districtReport.RdDistrictInfoService;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbSysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 片区信息-控制层实现
 *
 * @author 吕伟峰
 * @email www.justintoforest@gamail.com
 * @url 
 * @date 2019-08-14
 */
@RestController("RdDistrictInfoController")
@RequestMapping("/api/rdDistrictInfo")
public class RdDistrictInfoController extends BaseController{
    @Autowired
    private RdDistrictInfoService rdDistrictInfoService;

    @Autowired
    private AccessTokenService accessTokenService;

    @Autowired
    private TbSysUserService tbSysUserService;

    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    public Result list(RdDistrictInfoDTO rdDistrictInfoDTO,
                       @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       HttpServletRequest request){
        return  rdDistrictInfoService.queryPageList(rdDistrictInfoDTO,page,size,getAccessToken());
    }

    @GetMapping(value = "/rdDistrictInfoBown", produces = "application/json;charset=utf-8")
    public Result rdDistrictInfoBown(
                       @RequestParam(value = "regionCode", required = false)String regionCode,
                       HttpServletRequest request){
        return  rdDistrictInfoService.rdDistrictInfoBown(regionCode,getAccessToken());
    }

    @GetMapping(value = "/projectMp", produces = "application/json;charset=utf-8")
    public Result projectMp(
            @RequestParam(value = "districtCode", required = false)String districtCode,
            HttpServletRequest request){
        return  rdDistrictInfoService.projectMp(districtCode,getAccessToken());
    }

    @GetMapping(value = "/districtNameMp", produces = "application/json;charset=utf-8")
    public Result districtNameMp(
            @RequestParam(value = "districtName", required = false)String districtName,
            HttpServletRequest request){
        return  rdDistrictInfoService.districtNameMp(districtName,getAccessToken());
    }

    @GetMapping(value = "/rdDistrictInfoAddOne", produces = "application/json;charset=utf-8")
    public Result rdDistrictInfoAddOne(@RequestParam(value = "regionCode", required = true)String regionCode,
                                       @RequestParam(value = "districtName", required = true)String districtName,
                            HttpServletRequest request){
        return  rdDistrictInfoService.rdDistrictInfoAddOne(regionCode,districtName);
    }

    @GetMapping(value = "/one", produces = "application/json;charset=utf-8")
    public Result queryById(@RequestParam(value = "id", required = true)Integer id,
                            HttpServletRequest request){
        return  rdDistrictInfoService.queryById(id);
    }

    @GetMapping(value = "/addonePrincipale", produces = "application/json;charset=utf-8")
    public Result addonePrincipale(@RequestParam(value = "id", required = true)Integer id,
                            HttpServletRequest request){
        return  rdDistrictInfoService.addonePrincipale(id);
    }

    @PostMapping(value = "/add")
    public Result add(RdDistrictInfoDTO rdDistrictInfoDTO, String projectNameList,
                      HttpServletRequest request){
        return  rdDistrictInfoService.add(rdDistrictInfoDTO,getAccessToken(request));
    }

    @GetMapping(value = "/queryByUid", produces = "application/json;charset=utf-8")
    public Result queryByUid(@RequestParam(value = "search", required = true)String search,
                                   HttpServletRequest request){
        return  tbSysUserService.queryByUid(search);
    }

    @PostMapping(value = "/addPrincipal")
    public Result addPrincipal(RdDistrictPrincipal rdDistrictPrincipal,
                               HttpServletRequest request){
        return  rdDistrictInfoService.addPrincipal(rdDistrictPrincipal,getAccessToken(request));
    }

    @DeleteMapping(value="/del")
    public Result del(@RequestParam(value = "id", required = true)Integer id,@RequestParam(value = "updateName", required = true)String updateName){
        return  rdDistrictInfoService.del(id,getAccessToken(),updateName);
    }

    @GetMapping(value = "/prCommId",produces="application/json;charset=utf-8")
    public Result prCommId(@RequestParam(value="commName",required=true)String commName){

        return rdDistrictInfoService.prCommId(commName);
    }

    @PutMapping(value = "/edit")
    public Result edit(RdDistrictInfoDTO rdDistrictInfoDTO, HttpServletRequest request){
        return  rdDistrictInfoService.edit(rdDistrictInfoDTO,getAccessToken(request));
    }

    @DeleteMapping(value="/deletePrincipal")
    public Result deletePrincipal(@RequestParam(value = "id", required = true)Integer id){
        return  rdDistrictInfoService.deletePrincipal(id,getAccessToken());
    }

    @DeleteMapping(value="/delpiliang")
    public Result delpiliang(
            @RequestParam(value="ids",required=false)String ids,@RequestParam(value = "updateName", required = true)String updateName){
        return  rdDistrictInfoService.delpiliang(ids,getAccessToken(),updateName);
    }

    @GetMapping(value = "/findRoleregionName", produces = "application/json;charset=utf-8")
    public Result findRoleregionName(HttpServletRequest request) {

        String accessToken = request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
        List<DupOrgan> result = rdDistrictInfoService.findDupCommByUserCode(token.getUserCode(),null,true);
        return Response.response(result);
        //return personnelCommenService.findRoleregionName(getAccessToken(request));
    }

    @GetMapping(value = "/findRoleregionAllName", produces = "application/json;charset=utf-8")
    public Result findAllRoleregionName(HttpServletRequest request) {
        try{
            //String accessToken = request.getParameter("AccessToken");
            //AccessToken token = accessTokenService.findByToken(accessToken);
            List<DupOrgan> result = rdDistrictInfoService.findAllDupCommByUserCode(null,null,true);
            return Response.response(result);
        }catch(Exception e){
            e.getMessage();
            return Response.responseFail("查询失败");
        }
        //return personnelCommenService.findRoleregionName(getAccessToken(request));
    }

}