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.service.IndustryService;
import io.renren.modules.admin.service.MeetingService;
import io.renren.modules.admin.service.SubjectService;
import io.renren.modules.admin.service.TypesOfAttendersService;
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;


    /**
     * 列表
     */
    @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){
        String key = (String) params.get("key");
        int p = Integer.valueOf(params.get("page").toString());
        int l = Integer.valueOf(params.get("limit").toString());
        PageUtils page = meetingService.queryList(p, l, key);

        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);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("admin:meeting:save")
    public R save(@RequestBody MeetingEntity meeting){
        String subjects = meeting.getSubjects();
        String industries = meeting.getIndustries();
        String attendersType = meeting.getAttendersType();

        boolean sub = subjects.contains(",");
        if (sub){
            String[] subject = subjects.split(",");
            for (String s : subject) {
                int i = subjectService.selectIsName(s);
                if(i==0){
                    SubjectEntity se = new SubjectEntity();
                    se.setName(s);
                    se.setCreateTime(new Date());
                    subjectService.save(se);
                }
            }
        }else{
            int i = subjectService.selectIsName(subjects);
            if(i==0){
                SubjectEntity se = new SubjectEntity();
                se.setName(subjects);
                se.setCreateTime(new Date());
                subjectService.save(se);
            }
        }


        boolean ind = industries.contains(",");

        if(ind){
            String[] industry = industries.split(",");
            for (String s : industry) {
                int i = industryService.selectIsName(s);
                if(i==0){
                    IndustryEntity in = new IndustryEntity();
                    in.setName(s);
                    in.setCreateTime(new Date());
                    industryService.save(in);
                }
            }
        }else{
            int i = industryService.selectIsName(industries);
            if(i==0){
                IndustryEntity in = new IndustryEntity();
                in.setName(industries);
                in.setCreateTime(new Date());
                industryService.save(in);
            }
        }



        meeting.setCreateTime(new Date());
        meeting.setCompanyId((long) 1);
        meetingService.save(meeting);
        Long meetingid = meeting.getId();

        boolean att = attendersType.contains(",");

        if(att){
            String[] attenderType = attendersType.split(",");
            for (String s : attenderType) {
                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);
                }
            }

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

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:meeting:update")
    public R update(@RequestBody MeetingEntity meeting){

        String subjects = meeting.getSubjects();
        String industries = meeting.getIndustries();
        String attendersType = meeting.getAttendersType();
        Long meetingid = meeting.getId();
        System.out.println("会议id"+meetingid);

        boolean sub = subjects.contains(",");
        if(sub){
            String[] subject = subjects.split(",");
            for (String s : subject) {
                int i = subjectService.selectIsName(s);
                if(i==0){
                    SubjectEntity se = new SubjectEntity();
                    se.setName(s);
                    se.setCreateTime(new Date());
                    subjectService.save(se);
                }
            }
        }else{
            int i = subjectService.selectIsName(subjects);
            if(i==0){
                SubjectEntity se = new SubjectEntity();
                se.setName(subjects);
                se.setCreateTime(new Date());
                subjectService.save(se);
            }
        }

        boolean ind = industries.contains(",");
        if(ind){
            String[] industry = industries.split(",");
            for (String s : industry) {
                int i = industryService.selectIsName(s);
                if(i==0){
                    IndustryEntity in = new IndustryEntity();
                    in.setName(s);
                    in.setCreateTime(new Date());
                    industryService.save(in);
                }
            }
        }else{
            int i = industryService.selectIsName(industries);
            if(i==0){
                IndustryEntity in = new IndustryEntity();
                in.setName(industries);
                in.setCreateTime(new Date());
                industryService.save(in);
            }
        }


        boolean att = attendersType.contains(",");
        if(att){
            String[] attenderType = attendersType.split(",");
            for (String s : attenderType) {
                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);
                }
            }
        }else{
            int i = typesOfAttendersService.selectIsnameAndId(attendersType, meetingid);
            if(i==0) {
                TypesOfAttendersEntity typesOfAttenders = new TypesOfAttendersEntity();
                typesOfAttenders.setMeetingId(meetingid);
                typesOfAttenders.setName(attendersType);
                typesOfAttenders.setCreateTime(new Date());
                typesOfAttendersService.save(typesOfAttenders);
            }
        }


        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();
    }
}
