package com.jpsoft.shinestar.modules.business.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.jpsoft.shinestar.modules.base.dto.CashierDTO;
import com.jpsoft.shinestar.modules.base.entity.GoodsInfo;
import com.jpsoft.shinestar.modules.base.entity.OrderInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;

import com.jpsoft.shinestar.modules.base.service.OrderInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.business.dao.CameraPurchaserDAO;
import com.jpsoft.shinestar.modules.business.entity.CameraPurchaser;
import com.jpsoft.shinestar.modules.business.service.CameraPurchaserService;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Transactional
@Component(value="cameraPurchaserService")
public class CameraPurchaserServiceImpl implements CameraPurchaserService {

	@Resource(name="cameraPurchaserDAO")
	private CameraPurchaserDAO cameraPurchaserDAO;

	@Autowired
	private PersonInfoService personInfoService;

	@Autowired
	private OrderInfoService orderInfoService;
	@Override
	public CameraPurchaser get(String id) {
		// TODO Auto-generated method stub
		return cameraPurchaserDAO.get(id);
	}

	@Override
	public CameraPurchaser getByPersonId(Long studentId) {
		return cameraPurchaserDAO.getByPersonId(studentId);
	}

	@Override
	public int insert(CameraPurchaser model) {
		// TODO Auto-generated method stub
		//model.setId(UUID.randomUUID().toString());
		
		return cameraPurchaserDAO.insert(model);
	}

	@Override
	public int update(CameraPurchaser model) {
		// TODO Auto-generated method stub
		return cameraPurchaserDAO.update(model);		
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return cameraPurchaserDAO.delete(id);
	}

	@Override
	public boolean exist(String id) {
		// TODO Auto-generated method stub
		int count = cameraPurchaserDAO.exist(id);
		
		return count > 0 ? true : false;
	}
	
	@Override
	public List<CameraPurchaser> list() {
		// TODO Auto-generated method stub
		return cameraPurchaserDAO.list();
	}

	@Override
	public Page<CameraPurchaser> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize,boolean count,List<Sort> sortList) {
        Page<CameraPurchaser> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            cameraPurchaserDAO.search(searchParams,sortList);
        });
        
        return page;
	}


	@Override
	public CashierDTO submit(  GoodsInfo goodsInfo,Map map )  {
		String payPersonId=map.get("payPersonId").toString();
		String personId=map.get("personId").toString();
		String paycode=null;

		PersonInfo personInfo = personInfoService.get(Long.valueOf(personId));


		CashierDTO cashierDTO = new CashierDTO();


		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setId(UUID.randomUUID().toString());
		orderInfo.setBody(goodsInfo.getName());
		orderInfo.setTotalFee(goodsInfo.getCost());
		orderInfo.setPayStatus(10);
		orderInfo.setOutOrderNo(com.jpsoft.shinestar.modules.common.utils.StringUtils.getOutTradeNo());
		orderInfo.setType(goodsInfo.getType());
		orderInfo.setOpenId(personInfo.getOpenId());
		orderInfo.setCreateTime(new Date());
		orderInfo.setCreateBy(personInfo.getName());
		orderInfo.setDelFlag(false);
		orderInfo.setPersonId(personInfo.getId());
		orderInfo.setPayPopedomId(payPersonId);
		orderInfo.setGoodsId(goodsInfo.getId());
		orderInfo.setCompanyId(personInfo.getCompanyId());
		orderInfoService.insert(orderInfo);


		cashierDTO.setId(orderInfo.getId());
		cashierDTO.setOutOrderNo(orderInfo.getOutOrderNo());
		cashierDTO.setTotalFee(orderInfo.getTotalFee());
 		cashierDTO.setName(personInfo.getName());

		cashierDTO.setNum(1);
		cashierDTO.setGoodsName(goodsInfo.getName());
		cashierDTO.setGoodsDesc(goodsInfo.getDesc());
		cashierDTO.setGoodsPic(goodsInfo.getPic());

		return cashierDTO;
	}
}