/**
 *
 */
package com.sifang.modules.taskdispatch.controller;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.dao.CrmPlatformEmpDao;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import com.sifang.modules.taskdispatch.entity.CrmReviewGroup;
import com.sifang.modules.taskdispatch.service.CrmReviewGroupMemberService;
import com.sifang.modules.taskdispatch.service.CrmReviewGroupService;
import com.sifang.modules.taskdispatch.vo.EditGroupReq;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 审单组Controller
 *
 * @author gaols
 */
@Controller
@RequestMapping(value = "${adminPath}/taskdispatch/crmReviewGroup")
public class CrmReviewGroupController extends BaseController {

    @Autowired
    private CrmReviewGroupService crmReviewGroupService;
    @Autowired
    CrmPlatformEmpDao platformEmpDao;
    @Autowired
    CrmReviewGroupMemberService reviewGroupMemberService;

    private static final Logger log = LoggerFactory.getLogger(CrmReviewGroupController.class);

    @RequestMapping(value = {"listData"})
    @ResponseBody
    public String listData(HttpServletRequest request) {
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> params = getParams(request);
        crmReviewGroupService.listData(page, params);
        return page.renderJson();
    }

    @RequestMapping(value = {"editPage"})
    public String editPage(@RequestParam("groupId") String groupId, @RequestParam("partnerId") String partnerId, Model model) {
        List<Record> emps = platformEmpDao.findNotInReviewGroup(groupId, partnerId);
        model.addAttribute("emps", emps);
        model.addAttribute("groupId", groupId);
        model.addAttribute("partnerId", partnerId);
        CrmReviewGroup reviewGroup = crmReviewGroupService.get(groupId);
        List<Record> groupMembers = reviewGroupMemberService.groupMembersWithoutLeader(groupId);
        model.addAttribute("members", groupMembers);
        model.addAttribute("group", reviewGroup);
        String st1 = reviewGroup.getWorkTimeSt1();
        String st2 = reviewGroup.getWorkTimeSt2();
        if (StringUtils.isNotBlank(st1)) {
            model.addAttribute("spt1", st1 + " - " + reviewGroup.getWorkTimeEnd1());
        }
        if (StringUtils.isNotBlank(st2)) {
            model.addAttribute("spt2", st2 + " - " + reviewGroup.getWorkTimeEnd2());
        }
        return "modules/yangguang/systemSetting/flowable/item/projreviewGroupEditPage";
    }

    @RequestMapping(value = {"edit"})
    @ResponseBody
    public Result editGroup(@RequestBody EditGroupReq editGroupReq) {
        try {
            Db.tx(new IAtom() {
                @Override
                public boolean run() throws SQLException {
                    crmReviewGroupService.editGroup(editGroupReq);
                    return true;
                }
            });
            return Result.ok();
        } catch (BusinessException ex) {
            log.error("保存失败", ex);
            return Result.fail(ex.getMessage());
        } catch (Exception ex) {
            log.error("保存失败", ex);
            return Result.fail("保存失败，请重试！");
        }
    }

}
