package io.renren.modules.app.controller;

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

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PathVariable;

import io.renren.common.utils.R;
import io.renren.modules.admin.entity.AttendersEntity;
import io.renren.modules.admin.entity.MeetingEntity;
import io.renren.modules.admin.entity.PaymentEntity;
import io.renren.modules.admin.service.AttendersService;
import io.renren.modules.admin.service.MeetingService;
import io.renren.modules.admin.service.PaymentService;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.dao.UserDao;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.UserService;

/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-21 15:10:27
 */
@RestController
@RequestMapping("app/order")
public class AppOrderController {
    @Autowired
    private AttendersService attendersService;
    @Autowired
    private UserService userService;
    @Autowired
    private PaymentService paymentService;
    
    /**
     * 订单列表
     */
    @Login
    @GetMapping("/list")
    public R orderList(@RequestAttribute("userId") Integer userId){
        System.out.println(userId);
        Long memberId = Long.valueOf(userId);

        List<Map<String, Object>> orderList = userService.queryOrderList(memberId);        

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

    /**
     * 保存
     */
    @Login
    @PostMapping("/save")
    public R save(@RequestBody Map<String, Object> params, @LoginUser UserEntity user){
        Long meetingId = 0L;
        if (params.containsKey("meetingId")) {
            meetingId = Long.valueOf(params.get("meetingId").toString());
        } else {
            return R.error("会议id 不可为空");
        }
        AttendersEntity attendersEntity = new AttendersEntity();
        attendersEntity.setMeetingId(meetingId);
        attendersEntity.setMemberId(user.getUserId());
        attendersEntity.setName(user.getTruename());
        attendersEntity.setPhone(user.getPhone());
        attendersEntity.setOrganization(user.getOrganization());
        attendersEntity.setEmail(user.getEmail());
        Integer room = params.containsKey("room") ? Integer.valueOf(params.get("room").toString()) : 0 ;
        attendersEntity.setRoom(room);
        Integer transfer = params.containsKey("transfer") ? Integer.valueOf(params.get("transfer").toString()) : 0 ;
        attendersEntity.setTransfer(transfer);
        attendersEntity.setCreateTime(new Date());
        
        boolean res = attendersService.save(attendersEntity);
        if (!res) {
            return R.error("未知错误");
        }
        // System.out.println(attendersEntity.getId());

        // 付款信息
        PaymentEntity paymentEntity = new PaymentEntity();
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String nowtime = sdf.format(new Date());
        // System.out.println(time);
        Integer fourRandom = Integer.valueOf((int)((Math.random()*9+1)*1000));
        paymentEntity.setOrderId("FY" + nowtime + fourRandom);
        paymentEntity.setMeetingId(meetingId);
        paymentEntity.setAttendersId(attendersEntity.getId());
        Long feeId = params.containsKey("feeId") ? Long.valueOf(params.get("feeId").toString()) : 0L ;
        paymentEntity.setFeeId(feeId);
        paymentEntity.setPayType(1);
        Integer taxType = params.containsKey("taxType") ? Integer.valueOf(params.get("taxType").toString()) : 0;
        paymentEntity.setTaxType(taxType);
        String taxTitle = params.containsKey("taxTitle") ? params.get("taxTitle").toString() : "";
        paymentEntity.setTaxTitle(taxTitle);
        String taxNumber = params.containsKey("taxNumber") ? params.get("taxNumber").toString() : "";
        paymentEntity.setTaxNumber(taxNumber);
        String addPhone = params.containsKey("addPhone") ? params.get("addPhone").toString() : "";
        paymentEntity.setAddrPhone(addPhone);
        String bankAddrAccount = params.containsKey("bankAddrAccount") ? params.get("bankAddrAccount").toString() : "";
        paymentEntity.setBankAddrAccount(bankAddrAccount);
        String taxAddress = params.containsKey("taxAddress") ? params.get("taxAddress").toString() : "";
        paymentEntity.setTaxAddress(taxAddress);
        paymentEntity.setCreateTime(new Date());

        try {
            paymentService.save(paymentEntity);
        } catch (Exception e) {
            attendersService.removeById(attendersEntity.getId());
            e.printStackTrace();
            return R.error("订单生成出错");
        }

        return R.ok();
    }

    @GetMapping("/infobyaid/{aid}")
    public R infoByAttendersId(@PathVariable("aid") Long aid){
        System.out.println(aid);
        Map<String, Object> orderInfo = userService.queryOrderInfoByAttendersId(aid);
        return R.ok().put("orderInfo", orderInfo);
    }

    // /**
    //  * 修改
    //  */
    // @Login
    // @PostMapping("/update")
    // public R update(@RequestBody AttendersEntity attenders){
	// 	attendersService.updateById(attenders);

    //     return R.ok();
    // }

    // /**
    //  * 删除
    //  */
    // @Login
    // @PostMapping("/delete")
    // public R delete(@RequestBody Long[] ids){
	// 	attendersService.removeByIds(Arrays.asList(ids));

    //     return R.ok();
    // }
}
