package com.sifang.modules.smallprogram;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CrmEntryCombinerBoxGroupService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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


@Controller
@RequestMapping(value="${adminPath}/main/boxSet")
public class SmallProgramBoxGroupSetController extends BaseExceptionController {

    @Autowired
    private CrmEntryCombinerBoxGroupService boxGroupService;

    //选择整村汇流-获取数据
    @ResponseBody
    @RequestMapping(value = "choseBoxGroupData")
    public Object choseBoxGroupData(String entryId){
        Result result=new Result();
        List<Record> list=boxGroupService.choiceBoxGroupList(entryId);
        result.setOk();
        return result.data(list);
    }

    //选择汇流组保存、或者新增保存
    @ResponseBody
    @RequestMapping(value = "saveOneOwner")
    public Result saveOneOwner(String entryId, String boxGroupId, HttpServletRequest request){
        Operator currentUser = getOperatorBy(request);
        String totalCount1=request.getParameter("totalCount");
        Integer totalCount=null;
        if(StringUtil.isNotBlank(totalCount1)){
            try{
                totalCount=Integer.valueOf(totalCount1);
            }catch (Exception e){
                return Result.fail("电站个数格式不正确！");
            }
        }
        boxGroupService.saveOneOwner(entryId,totalCount,boxGroupId,currentUser);
        return Result.ok();
    }
}
