package io.renren.modules.admin.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.admin.entity.IndustryEntity;
import io.renren.modules.admin.entity.MeetingEntity;
import io.renren.modules.admin.entity.SubjectEntity;
import io.renren.modules.admin.entity.TypesOfAttendersEntity;
import io.renren.modules.admin.entity.EmployeeEntity;
import io.renren.modules.admin.form.MeetingForm;
import io.renren.modules.admin.service.IndustryService;
import io.renren.modules.admin.service.MeetingService;
import io.renren.modules.admin.service.SubjectService;
import io.renren.modules.admin.service.TypesOfAttendersService;
import io.renren.modules.admin.service.EmployeeService;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 会议表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-20 14:59:39
 */
@RestController
@RequestMapping("admin/meeting")
public class MeetingController {
    @Autowired
    private MeetingService meetingService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private IndustryService industryService;

    @Autowired
    private TypesOfAttendersService typesOfAttendersService;

@Autowired
    private EmployeeService employeeService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("admin:meeting:list")
    public R list(@RequestParam Map<String, Object> params){
        // PageUtils page = meetingService.queryPage(params);
        List<MeetingEntity> list = meetingService.list();
        return R.ok().put("list", list);
    }

    /**
     * 列表2
     */
    @RequestMapping("/listtest")
    @RequiresPermissions("admin:meeting:list")
    public R listtest(@RequestParam Map<String, Object> params){
        PageUtils page = meetingService.myQueryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("admin:meeting:info")
    public R info(@PathVariable("id") Long id){
		MeetingEntity meeting = meetingService.getById(id);
        Long sId = meeting.getServiceId();
        if (sId == null || sId == 0) {
              meeting.setServiceName("");
        } else{
            EmployeeEntity employee = employeeService.getById(sId);
            meeting.setServiceName(employee.getTruename());
        }
        
        return R.ok().put("meeting", meeting);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequiresPermissions("admin:meeting:save")
    public R save(@RequestBody MeetingForm meetingForm){
        String[] subjects = meetingForm.getSubjects();
        String[] industries = meetingForm.getIndustries();
        String[] attendersType = meetingForm.getAttendersType();
        for (String s : subjects) {
            int i = subjectService.selectIsName(s);
            if(i==0){
                SubjectEntity se = new SubjectEntity();
                se.setName(s);
                se.setCreateTime(new Date());
                subjectService.save(se);
            }
        }

        for (String s : industries) {
            int i = industryService.selectIsName(s);
            if(i==0){
                IndustryEntity in = new IndustryEntity();
                in.setName(s);
                in.setCreateTime(new Date());
                industryService.save(in);
            }
        }

        MeetingEntity meeting= new MeetingEntity();
        meeting.setCompanyId((long) 1);
        meeting.setNameCn(meetingForm.getNameCn());
        meeting.setNameEn(meetingForm.getNameEn());
        meeting.setAddress(meetingForm.getAddress());
        meeting.setStartTime(meetingForm.getStartTime());
        meeting.setEndTime(meetingForm.getEndTime());
        meeting.setOfficeWebsite(meetingForm.getOfficeWebsite());
        meeting.setTitlePicture(meetingForm.getTitlePicture());
        meeting.setSubjects(StringUtils.join(meetingForm.getSubjects(), ","));
        meeting.setIndustries(StringUtils.join(meetingForm.getIndustries(), ","));
        meeting.setAttendersType(StringUtils.join(meetingForm.getAttendersType(), ","));
        meeting.setServiceId(meetingForm.getServiceId());
        meeting.setServiceName(meetingForm.getServiceName());
        meeting.setContactIds(StringUtils.join(meetingForm.getContactIds(), ","));
        meeting.setContactNames(StringUtils.join(meetingForm.getContactNames(), ","));
        meeting.setOnlineRegDeadline(meetingForm.getOnlineRegDeadline());
        meeting.setOnsiteRegDeadline(meetingForm.getOnsiteRegDeadline());
        meeting.setIntroduction(meetingForm.getIntroduction());
        meeting.setCreateTime(new Date());
        meetingService.save(meeting);
        Long meetingid = meeting.getId();

        for (String s : attendersType) {
            int i = typesOfAttendersService.selectIsnameAndId(s, meetingid);
            if(i==0) {
                TypesOfAttendersEntity typesOfAttenders = new TypesOfAttendersEntity();
                typesOfAttenders.setMeetingId(meetingid);
                typesOfAttenders.setName(s);
                typesOfAttenders.setCreateTime(new Date());
                typesOfAttendersService.save(typesOfAttenders);
            }
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:meeting:update")
    public R update(@RequestBody MeetingForm meetingForm){
        String[] subjects = meetingForm.getSubjects();
        String[] industries = meetingForm.getIndustries();
        String[] attendersType = meetingForm.getAttendersType();
        Long meetingid = meetingForm.getId();

        for (String s : subjects) {
            int i = subjectService.selectIsName(s);
            if(i==0){
                SubjectEntity se = new SubjectEntity();
                se.setName(s);
                se.setCreateTime(new Date());
                subjectService.save(se);
            }
        }

        for (String s : industries) {
            int i = industryService.selectIsName(s);
            if(i==0){
                IndustryEntity in = new IndustryEntity();
                in.setName(s);
                in.setCreateTime(new Date());
                industryService.save(in);
            }
        }

        for (String s : attendersType) {
            int i = typesOfAttendersService.selectIsnameAndId(s, meetingid);
            if(i==0) {
                TypesOfAttendersEntity typesOfAttenders = new TypesOfAttendersEntity();
                typesOfAttenders.setMeetingId(meetingid);
                typesOfAttenders.setName(s);
                typesOfAttenders.setCreateTime(new Date());
                typesOfAttendersService.save(typesOfAttenders);
            }
        }

        MeetingEntity meeting= new MeetingEntity();
        meeting.setId(meetingid);
        meeting.setCompanyId((long) 1);
        meeting.setNameCn(meetingForm.getNameCn());
        meeting.setNameEn(meetingForm.getNameEn());
        meeting.setAddress(meetingForm.getAddress());
        meeting.setStartTime(meetingForm.getStartTime());
        meeting.setEndTime(meetingForm.getEndTime());
        meeting.setOfficeWebsite(meetingForm.getOfficeWebsite());
        meeting.setTitlePicture(meetingForm.getTitlePicture());
        meeting.setSubjects(StringUtils.join(meetingForm.getSubjects(), ","));
        meeting.setIndustries(StringUtils.join(meetingForm.getIndustries(), ","));
        meeting.setAttendersType(StringUtils.join(meetingForm.getAttendersType(), ","));
        meeting.setServiceId(meetingForm.getServiceId());
        meeting.setServiceName(meetingForm.getServiceName());
        meeting.setContactIds(StringUtils.join(meetingForm.getContactIds(), ","));
        meeting.setContactNames(StringUtils.join(meetingForm.getContactNames(), ","));
        meeting.setOnlineRegDeadline(meetingForm.getOnlineRegDeadline());
        meeting.setOnsiteRegDeadline(meetingForm.getOnsiteRegDeadline());
        meeting.setIntroduction(meetingForm.getIntroduction());
        meeting.setModifyTime(new Date());
		meetingService.updateById(meeting);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("admin:meeting:delete")
    public R delete(@RequestBody Long[] ids){
		meetingService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/status")
    @RequiresPermissions("admin:meeting:status")
    public R status(@RequestBody Map<String, Object> param){
        Long id = Long.valueOf(param.get("id").toString());
        Integer status = Integer.valueOf(param.get("status").toString());

        meetingService.updateStatus(id, status);
        return R.ok();
    }
}
