package io.renren.modules.app.controller;

import io.renren.common.utils.MyUtils;
import io.renren.common.utils.R;
import io.renren.modules.admin.entity.AttendersEntity;
import io.renren.modules.admin.entity.AttendersviewEntity;
import io.renren.modules.admin.entity.MemberEntity;
import io.renren.modules.admin.service.AttendersService;
import io.renren.modules.admin.service.AttendersviewService;
import io.renren.modules.admin.service.MemberService;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;

/**
 * APP测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app/attenders")
public class AppAttendersController {
    @Autowired
    private AttendersService attendersService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private AttendersviewService attendersviewService;

    @GetMapping("notToken")
    public R notToken(){
        return R.ok().put("msg", "无需token也能访问。。。");
    }

    @PostMapping("save")
    public R save(@RequestBody AttendersviewEntity attendersform){
        Long meetingId = attendersform.getMeetingId();
        if (meetingId == 0L) {
            return R.error("会议id不可为空");
        }

        String symbol = MyUtils.md5(attendersform.getTruename() + attendersform.getPhone());

        MemberEntity member = memberService.getBySymbol(symbol);

        // 存会员表(个人信息, 开票信息)
        if (member == null) {
            member = new MemberEntity();
        }
        member.setTruename(attendersform.getTruename());
        member.setOrganization(attendersform.getOrganization());
        member.setPosition(attendersform.getPosition());
        member.setPhone(attendersform.getPhone());
        member.setEmail(attendersform.getEmail());
        member.setSex(attendersform.getSex());
        member.setRemark(attendersform.getRemark());
        member.setTaxType(attendersform.getTaxType());
        member.setTaxTitle(attendersform.getTaxTitle());
        member.setTaxNumber(attendersform.getTaxNumber());
        member.setCompanyAddress(attendersform.getCompanyAddress());
        member.setCompanyPhone(attendersform.getCompanyPhone());
        member.setBankAccount(attendersform.getBankAccount());
        member.setMailingAddress(attendersform.getMailingAddress());
        member.setPostcode(attendersform.getPostcode());

        if (member.getId()==null || member.getId() == 0L) {
            memberService.saveMember(member);
        } else {
            memberService.updateMember(member);
        }

        Long memberId = member.getId();
        // 存参会信息
        AttendersEntity attenders = new AttendersEntity();
        attenders.setMemberId(memberId);
        attenders.setMeetingId(meetingId);
        attenders.setMemberType(attenders.getMemberType());
        attenders.setFeeType(attenders.getFeeType());
        attenders.setPaperType(attenders.getPaperType());
        attenders.setIsReport(attenders.getIsReport());
        attenders.setReportTitle(attenders.getReportTitle());
        attenders.setHotelType(attenders.getHotelType());
        attenders.setRoomType(attenders.getRoomType());
        attenders.setCheckInDate(attenders.getCheckInDate());
        attenders.setCheckOutDate(attenders.getCheckOutDate());
        attenders.setPayType(attenders.getPayType());
        attenders.setIsPay(attenders.getIsPay());
        attenders.setMoney(attenders.getMoney());
        attenders.setCreateTime(new Date());

        attendersService.save(attenders);

        return R.ok();
    }

    @GetMapping("/infobysymbol/{token}")
    public R notToken(@PathVariable("symbol") String symbol){
        // AttendersviewEntity attenders = attendersviewService.getBySymbol(token);
        // return R.ok().put("attenders", attenders);
        return null;
    }

}


