package com.gszc.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gszc.entity.*;
import com.gszc.mapper.*;
import com.gszc.service.ICustomService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
*
* 服务实现类
*
*
* @author ky
* @since 2020-04-27
*/
@Service
@Transactional
public class CustomServiceImpl extends ServiceImpl implements ICustomService {
@Autowired
CustomMapper customMapper;
@Autowired
CustomSupportMapper customSupportMapper;
@Autowired
MiniUserMapper miniUserMapper;
@Autowired
ReceiveAddressServiceImpl receiveAddressService;
@Autowired
PcUserMapper pcUserMapper;
@Autowired
InvoiceMapper invoiceMapper;
@Autowired
InvoiceHeaderMapper invoiceHeaderMapper;
public void customRegister(Custom custom,List customSupports){
String uuid = UUID.randomUUID().toString();
custom.setId(uuid);
custom.setCreateDate( new Date());
custom.setModifyDate( new Date());
custom.setStatusCode("todo");
customMapper.insert(custom);
if(customSupports.size()>0){
for(CustomSupport customSupport:customSupports){
String id = UUID.randomUUID().toString();
customSupport.setId(id);
customSupport.setCreateDate( new Date());
customSupport.setModifyDate( new Date());
customSupport.setCustomId(uuid);
customSupportMapper.insert(customSupport);
}
}
}
/**
* 用户可开票(已认证)公司
* @param userId
* @return
*/
public List invoiceCustomList(String userId){
List customs = customMapper.selectList(new EntityWrapper().eq("mini_user_id", userId).eq("status_code","enable"));
return customs;
}
public List customList(String userId){
List customs = customMapper.selectList(new EntityWrapper().eq("mini_user_id", userId));
return customs;
}
public JSONObject customDetail(String customId){
JSONObject jsonObject = new JSONObject();
Custom custom = customMapper.selectById(customId);
List customSupports = customSupportMapper.selectList(new EntityWrapper().eq("custom_id", customId));
jsonObject.put("custom",custom);
jsonObject.put("customSupports",customSupports);
return jsonObject;
}
/**
* 待接单
* @param pageNum
* @param pageSize
* @return
*/
public List pcTodoCustomList(Integer pageNum,Integer pageSize){
List customs = customMapper.selectPage(new RowBounds((pageNum-1)*pageSize,pageSize),new EntityWrapper().eq("status_code", "todo"));
return customs;
}
/**
* 待认证
* @param pageNum
* @param pageSize
* @return
*/
public List pcDisableCustomList(Integer pageNum,Integer pageSize){
List customs = customMapper.selectPage(new RowBounds((pageNum-1)*pageSize,pageSize),new EntityWrapper().eq("status_code", "disable"));
return customs;
}
/**
* 已认证
* @param pageNum
* @param pageSize
* @return
*/
public List pcEnableCustomList(Integer pageNum,Integer pageSize){
List customs = customMapper.selectPage(new RowBounds((pageNum-1)*pageSize,pageSize),new EntityWrapper().eq("status_code", "enable"));
return customs;
}
/**
* 接单
* @param customId
* @param userId
*/
public void doCustom(String customId,String userId){
Custom custom = new Custom();
custom.setId(customId);
custom.setModifyDate( new Date());
custom.setCopycatId(userId);
custom.setStatusCode("disable");
customMapper.updateById(custom);
}
/**
* 修改接单员
* @param customId
* @param userId
*/
public void changeCopycat(String customId,String userId){
Custom custom = new Custom();
custom.setId(customId);
custom.setModifyDate( new Date());
custom.setCopycatId(userId);
customMapper.updateById(custom);
}
/**
* 跟进备注
* @param customId
*/
public void customNotice(String customId, String notice, String noticeContent){
Custom custom = new Custom();
custom.setId(customId);
custom.setModifyDate( new Date());
custom.setNotice(notice);
custom.setNoticeContent(noticeContent);
customMapper.updateById(custom);
}
/**
* 认证
* @param customId
*/
public void enableCustom(String customId, String businessLicense, String socialCreditCode,Long serviceBeginDate,Long serviceEndDate){
Custom custom = new Custom();
custom.setId(customId);
custom.setModifyDate( new Date());
custom.setBusinessLicense(businessLicense);
custom.setSocialCreditCode(socialCreditCode);
custom.setStatusCode("enable");
custom.setServiceBeginDate(new Date(serviceBeginDate));
custom.setServiceEndDate(new Date(serviceEndDate));
customMapper.updateById(custom);
}
public JSONObject enableCustomDetail(String customId){
JSONObject jsonObject = new JSONObject();
Custom custom = customMapper.selectById(customId);
String miniUserId = custom.getMiniUserId();
MiniUser miniUser = miniUserMapper.selectById(miniUserId);
List receiveAddressList = receiveAddressService.addressList(miniUserId);
String copycatId = custom.getCopycatId();
PcUser pcUser = pcUserMapper.selectById(copycatId);
jsonObject.put("custom",custom);
jsonObject.put("miniUser",miniUser);
jsonObject.put("receiveAddressList",receiveAddressList);
jsonObject.put("pcUser",pcUser);
//年度
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.YEAR, -1);
Date y = c.getTime();
List yearInvoiceList = invoiceMapper.selectList(new EntityWrapper().eq("custom_id", customId).ge("create_date", y));
//月度
c.setTime(new Date());
c.add(Calendar.MONTH, -1);
Date m = c.getTime();
List monthInvoiceList = invoiceMapper.selectList(new EntityWrapper().eq("custom_id", customId).ge("create_date", m));
BigDecimal yearMoney = new BigDecimal("0");
BigDecimal monthMoney = new BigDecimal("0");
for(Invoice invoice:yearInvoiceList){
BigDecimal money = invoice.getMoney();
yearMoney = yearMoney.add(money);
}
for(Invoice invoice:monthInvoiceList){
BigDecimal money = invoice.getMoney();
monthMoney = monthMoney.add(money);
}
jsonObject.put("yearMoney",yearMoney);
jsonObject.put("monthMoney",monthMoney);
jsonObject.put("yearCount",yearInvoiceList.size());
return jsonObject;
}
public List enableCustomDetailInvoiceList(String customId,Integer pageNum,Integer pageSize){
List invoiceList = invoiceMapper.selectPage(new RowBounds((pageNum-1)*pageSize,pageSize),new EntityWrapper().eq("custom_id", customId));
return invoiceList;
}
}