package com.goafanti.business.controller;

import com.goafanti.business.bo.InputRestrictProject;
import com.goafanti.business.bo.RestrictProjectPageList;
import com.goafanti.business.bo.UserRestrictProjectBo;
import com.goafanti.business.service.RestrictProjectService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.utils.ParamUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/restrict/Project")
public class RestrictProjectController extends CertifyApiController {
    @Resource
    private RestrictProjectService restrictProjectService;

    /**
     * 添加限制项目
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@Validated InputRestrictProject in, BindingResult bindingResult){
        Result res = res();
        if (bindingResult.hasErrors()) {
            res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
                    ParamUtils.getParamName(in,bindingResult.getFieldError().getField())));
            return res;
        }
        if (restrictProjectService.checkMax(in.getUid())){
            return res.error(buildError("领取失败！您的“私有客户/私有限定项目”总计已超出最大限制，请移出相关数据后，再来领取！"));
        }
        return res.data(restrictProjectService.add(in));
    }

    /**
     * 限制项目列表
     * @param in
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(InputRestrictProject in){
        Result res = res();
        if (in.getUid()==null){
            return res.error(buildError("客户不能为空"));
        }
        return res.data(restrictProjectService.list(in));
    }

    /**
     * 限定项目修改
     * @param id 编号
     * @param type 0=移除，1=转交
     * @param takeAid 接受者
     */
    @RequestMapping(value ="/update",method = RequestMethod.POST)
    public Result update(String id,Integer type,String takeAid){
        Result res = res();
        return res.data(restrictProjectService.update(id,type,takeAid));
    }

    /**
     * 限定项目分页列表
     * @param in
     * @return
     */
    @RequestMapping(value = "/pageList", method = RequestMethod.GET)
    public Result<RestrictProjectPageList> pageList(InputRestrictProject in){
        Result res = res();
        return res.data(restrictProjectService.pageList(in));
    }

    /**
     * 限定项目跟进
     */
    @RequestMapping(value = "/getFollowDetails", method = RequestMethod.GET)
    public Result<RestrictProjectPageList> getFollowDetails(String uid){
        return res().data(restrictProjectService.getFollowDetails(uid));
    }

    /**
     * 限定项目客户情况查询
     */
    @RequestMapping(value = "/getRestrictProjectUser",method = RequestMethod.GET)
    public Result<List<UserRestrictProjectBo>> getRestrictProjectUser (String ids ){
        return res().data(restrictProjectService.getRestrictProjectUser(ids));
    }
}
