package com.ssm.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmStoreRoomServiceDao;
import com.ssm.manage.dao.SsmStoreSettlementDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.StoreSettlementRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.*;
import com.ssm.util.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SsmStoreSettlementServiceImpl  extends BaseServiceImpl<StoreSettlement> implements SsmStoreSettlementService {

    @Autowired
    private SsmStoreSettlementDao dao;

    @Autowired
    private SsmClockServiceService ssmClockServiceService;

    @Autowired
    private SsmProjectService ssmProjectService;
    @Autowired
    private SsmStoreRoomService ssmStoreRoomService;
    @Autowired
    private SsmStoreRoomHandService ssmStoreRoomHandService;
    @Autowired
    private SsmServiceOrderService ssmServiceOrderService;
    @Autowired
    private SsmStoreSettelnmentProjectService ssmStoreSettelnmentProjectService;

    private static String staffKey = "staffKey";

    @Autowired
    private RedisClient<StaffResponse> redisClient;

    @Override
    public SsmStoreSettlementDao getDao() {
        return dao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(List<StoreSettlementRequest> serviceOrdersRequest, String phone) throws SmsException {
        int a=0;
        for (StoreSettlementRequest storeSettlementRequest : serviceOrdersRequest) {
            if(null == storeSettlementRequest.getId()){
                StoreSettlement storeSettlement = new StoreSettlement();
                BeanUtils.copyProperties(storeSettlementRequest,storeSettlement);
                if(null == storeSettlement.getServiceTechnicianOneStaffId()){
                    throw new SmsException(ResponseCode.MSG,"未选择服务技师");
                }
                if(null == storeSettlement.getProjectId()){
                    throw new SmsException(ResponseCode.MSG,"未选择服务项目");
                }
                BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(storeSettlement.getProjectId());
                baseProject1.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                storeSettlement.setProjectName(baseProject1.getProjectName());
                storeSettlement.setProjectName(baseProject1.getProjectName());
                storeSettlement.setAchievementZhonggong(baseProject1.getAchievementZhonggong());
                storeSettlement.setAchievementTechnician(baseProject1.getAchievementTechnician());
                storeSettlement.setAchievementSmall(baseProject1.getAchievementSmall());
                storeSettlement.setCommissionLarge(baseProject1.getCommissionLarge());
                storeSettlement.setCommissionZhonggong(baseProject1.getCommissionZhonggong());
                storeSettlement.setCommissionTechnician(baseProject1.getCommissionTechnician());
                storeSettlement.setCommissionSmall(baseProject1.getCommissionSmall());
                storeSettlement.setCardAchievementLarge(baseProject1.getCardAchievementLarge());
                storeSettlement.setCardAchievementZhonggong(baseProject1.getCardAchievementZhonggong());
                storeSettlement.setCardAchievementTechnician(baseProject1.getCardAchievementTechnician());
                storeSettlement.setCardAchievementSmall(baseProject1.getCardAchievementSmall());
                storeSettlement.setCardCommissionLarge(baseProject1.getCardCommissionLarge());
                storeSettlement.setCardCommissionZhonggong(baseProject1.getCardCommissionZhonggong());
                storeSettlement.setCardCommissionTechnician(baseProject1.getCardCommissionTechnician());
                storeSettlement.setCardCommissionSmall(baseProject1.getCardCommissionSmall());
                storeSettlement.setRenew(storeSettlementRequest.getRenew());
                storeSettlement.setNoDiscount(baseProject1.getNoDiscount());
                storeSettlement.setPrice(baseProject1.getPrice());
                storeSettlement.setMinumumPrice(baseProject1.getMinumumPrice());
                storeSettlement.setPriceMember(baseProject1.getPriceMember());
                storeSettlement.setMinumumPriceMember(baseProject1.getMinumumPriceMember());
                storeSettlement.setProjectNum(baseProject1.getProjectNum());
                storeSettlement.setServiceTechnicianOneRatio(BigDecimal.valueOf(100));
                storeSettlement.setMarketComm(baseProject1.getMarketComm());
                storeSettlement.setClockComm(baseProject1.getClockComm());
                storeSettlement.setRoundClockComm(baseProject1.getRoundClockComm());
                storeSettlement.setCardMarketComm(baseProject1.getCardMarketComm());
                storeSettlement.setCardClockComm(baseProject1.getCardClockComm());
                storeSettlement.setCardRoundClockComm(baseProject1.getCardRoundClockComm());
                storeSettlement.setProjectNature(0);
                storeSettlement.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
                StaffResponse staffResponse = new StaffResponse();
                Set<String> keys = redisClient.keys(getStaffKey(phone) + "*");
                //从redis查询 设置录单人信息 app使用
                if (keys.size() > 0){
                    String key =  new ArrayList<>(keys).get(0);
                    staffResponse = redisClient.get(key);
                }
                storeSettlement.setInputId(staffResponse.getId());
                storeSettlement.setInputBy(staffResponse.getStaffName());
                storeSettlement.setInputDate(new Date());
                storeSettlement.setState(0);
                storeSettlement.setServicingTime(storeSettlementRequest.getServicingTime());
                int insert =0;
                try {
                    insert = dao.insert(storeSettlement);
                }catch(Exception e){
                    System.out.println(e.getMessage());
                }
                if(insert>0){
                    a++;
                    StoreRoom storeRoom = ssmStoreRoomService.selectByPrimaryKey(storeSettlement.getRoomId());
                    if(storeRoom.getOnState() ==0 ){
                        storeRoom.setOnState(1);
                        int i = ssmStoreRoomService.updateNotNull(storeRoom);
                        if(i==0){
                            throw new SmsException(ResponseCode.MSG,"修改房间状态失败");
                        }
                    }
                    StoreRoomHand storeRoomHand = ssmStoreRoomHandService.selectByPrimaryKey(storeSettlement.getRoomHandId());
                    if(storeRoomHand.getState() ==0 ){
                        storeRoomHand.setState(1);
                        int i = ssmStoreRoomHandService.updateNotNull(storeRoomHand);
                        if(i==0){
                            throw new SmsException(ResponseCode.MSG,"占用手牌失败");
                        }
                    }
                    ServiceOrder serviceOrder = new ServiceOrder();
                    serviceOrder.setStaffId(storeSettlement.getServiceTechnicianOneStaffId());
                    serviceOrder.setStoreId(storeSettlement.getStoreId());
                    ServiceOrder serviceOrder1 = ssmServiceOrderService.selectOne(serviceOrder);
                    if(serviceOrder1.getState()==0){
                        serviceOrder1.setState(1);
                        ssmServiceOrderService.updateNotNull(serviceOrder1);
                    }
                }else{
                    throw new SmsException(ResponseCode.MSG,"保存失败");
                }
            }else{
                StoreSettlement storeSettlement = new StoreSettlement();
                BeanUtils.copyProperties(storeSettlementRequest,storeSettlement);
                StoreSettlement storeSettlement1 = dao.selectByPrimaryKey(storeSettlement.getId());
                if(!storeSettlement.getProjectId().equals(storeSettlement1.getProjectId())){
                    BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(storeSettlement.getProjectId());
                    storeSettlement1.setProjectName(baseProject1.getProjectName());
                    storeSettlement1.setProjectName(baseProject1.getProjectName());
                    storeSettlement1.setAchievementZhonggong(baseProject1.getAchievementZhonggong());
                    storeSettlement1.setAchievementTechnician(baseProject1.getAchievementTechnician());
                    storeSettlement1.setAchievementSmall(baseProject1.getAchievementSmall());
                    storeSettlement1.setCommissionLarge(baseProject1.getCommissionLarge());
                    storeSettlement1.setCommissionZhonggong(baseProject1.getCommissionZhonggong());
                    storeSettlement1.setCommissionTechnician(baseProject1.getCommissionTechnician());
                    storeSettlement1.setCommissionSmall(baseProject1.getCommissionSmall());
                    storeSettlement1.setCardAchievementLarge(baseProject1.getCardAchievementLarge());
                    storeSettlement1.setCardAchievementZhonggong(baseProject1.getCardAchievementZhonggong());
                    storeSettlement1.setCardAchievementTechnician(baseProject1.getCardAchievementTechnician());
                    storeSettlement1.setCardAchievementSmall(baseProject1.getCardAchievementSmall());
                    storeSettlement1.setCardCommissionLarge(baseProject1.getCardCommissionLarge());
                    storeSettlement1.setCardCommissionZhonggong(baseProject1.getCardCommissionZhonggong());
                    storeSettlement1.setCardCommissionTechnician(baseProject1.getCardCommissionTechnician());
                    storeSettlement1.setCardCommissionSmall(baseProject1.getCardCommissionSmall());
                    storeSettlement1.setRenew(storeSettlement.getRenew());
                    storeSettlement1.setServicingTime(baseProject1.getServicingTime());
                    storeSettlement1.setNoDiscount(baseProject1.getNoDiscount());

                    storeSettlement1.setTransactionPrice(storeSettlement1.getPrice().multiply(BigDecimal.valueOf(storeSettlement1.getNumberTimes())));
                    storeSettlement1.setServicingTime(baseProject1.getServicingTime()*storeSettlement1.getNumberTimes());
                }

                if(null != storeSettlement.getServiceTechnicianOneStaffId()){
                    //判断是否有更换技师
                    if(!storeSettlement1.getServiceTechnicianOneStaffId().equals(storeSettlement.getServiceTechnicianOneStaffId())){
                        ServiceOrder serviceOrder = new ServiceOrder();
                        serviceOrder.setStaffId(storeSettlement1.getServiceTechnicianOneStaffId());
                        serviceOrder.setStoreId(storeSettlement1.getStoreId());
                        ServiceOrder serviceOrder1 = ssmServiceOrderService.selectOne(serviceOrder);
                        serviceOrder1.setState(0);
                        int i = ssmServiceOrderService.updateNotNull(serviceOrder1);
                        if(i>0){
                            storeSettlement1.setServiceTechnicianOneStaffId(storeSettlement.getServiceTechnicianOneStaffId());
                            storeSettlement1.setTechnicianOneNumber(storeSettlement.getTechnicianOneNumber());
                            storeSettlement1.setServiceTechnicianOneStaffName(storeSettlement.getServiceTechnicianOneStaffName());
                            storeSettlement1.setServiceTechnicianOneIdentityCard(storeSettlement.getServiceTechnicianOneIdentityCard());
                            storeSettlement1.setServiceTechnicianOneRatio(storeSettlement.getServiceTechnicianOneRatio());
                            ServiceOrder serviceOrders = new ServiceOrder();
                            serviceOrders.setStaffId(storeSettlement1.getServiceTechnicianOneStaffId());
                            serviceOrders.setStoreId(storeSettlement1.getStoreId());
                            ServiceOrder serviceOrder2 = ssmServiceOrderService.selectOne(serviceOrders);
                            serviceOrder2.setState(1);
                            int i1 = ssmServiceOrderService.updateNotNull(serviceOrder2);
                            if(i1==0){
                                throw new SmsException(ResponseCode.MSG,"更换技师失败");
                            }
                        }else{
                            throw new SmsException(ResponseCode.MSG,"更换技师失败");
                        }
                    }
                }

                if(null != storeSettlement.getStaffId()){
                    //判断是否有更换推销人
                    if(!storeSettlement1.getStaffId().equals(storeSettlement.getStaffId())){
                        storeSettlement1.setStaffId(storeSettlement.getStaffId());
                        storeSettlement1.setStaffName(storeSettlement.getStaffName());
                        storeSettlement1.setStaffNum(storeSettlement.getStaffNum());
                        storeSettlement1.setBrandNumber(storeSettlement.getBrandNumber());
                    }
                }

                if(storeSettlement1.getNumberTimes()!= storeSettlement.getNumberTimes()){
                    BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(storeSettlement.getProjectId());
                    storeSettlement1.setNumberTimes(storeSettlement.getNumberTimes());
                    storeSettlement1.setTransactionPrice(storeSettlement.getPrice().multiply(BigDecimal.valueOf(storeSettlement.getNumberTimes())));
                    storeSettlement1.setServicingTime(baseProject1.getServicingTime()*storeSettlement.getNumberTimes());
                }
                int update = dao.updateByPrimaryKeySelective(storeSettlement1);
                if(update>0){
                    a++;
                }
            }
        }
        if(a==serviceOrdersRequest.size()){
            return a;
        }else{
            throw new SmsException(ResponseCode.MSG,"保存数据异常");
        }
    }

    @Override
    public int update(StoreSettlementRequest serviceOrdersRequest, String phone) throws SmsException {
        StoreSettlement storeSettlement = new StoreSettlement();
        BeanUtils.copyProperties(serviceOrdersRequest,storeSettlement);
        StoreSettlement storeSettlement1 = dao.selectByPrimaryKey(storeSettlement.getId());

        //判断是否有更换房间
        if(!storeSettlement.getRoomHandId().equals(storeSettlement1.getRoomHandId())){
            StoreRoomHand storeRoomHand = new StoreRoomHand();
            storeRoomHand.setId(storeSettlement.getRoomHandId());
            StoreRoomHand storeRoomHand1 = ssmStoreRoomHandService.selectByPrimaryKey(storeRoomHand.getId());
            if(storeRoomHand1.getState()==1){
                storeSettlement1.setRoomId(storeSettlement.getRoomId());
                storeSettlement1.setRoomNum(storeSettlement.getRoomNum());
                storeSettlement1.setRoomName(storeSettlement.getRoomName());
                storeSettlement1.setRoomHandId(storeSettlement.getRoomHandId());
                storeSettlement1.setRoomHandNum(storeSettlement.getRoomHandNum());
                storeRoomHand1.setState(1);
                int i = ssmStoreRoomHandService.updateNotNull(storeRoomHand1);
                if(i==0){
                    throw new SmsException(ResponseCode.MSG,"更换的手牌失败");
                }
                StoreRoom storeRoom = new StoreRoom();
                storeRoom.setId(storeSettlement.getRoomId());
                StoreRoom storeRoom1 = ssmStoreRoomService.selectByPrimaryKey(storeRoom.getId());
                if(storeRoom1.getOnState()!=1){
                    storeRoom1.setState(1);
                    int i1 = ssmStoreRoomService.updateNotNull(storeRoom1);
                }
            }else{
                throw new SmsException(ResponseCode.MSG,"更换的手牌正在被占用");
            }
        }

        if(null != storeSettlement.getProjectId()){
            if(!storeSettlement.getProjectId().equals(storeSettlement1.getProjectId())){
                if(null == storeSettlement1.getServiceStartDate()){
                    BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(storeSettlement.getProjectId());
                    storeSettlement1.setProjectName(baseProject1.getProjectName());
                    storeSettlement1.setProjectName(baseProject1.getProjectName());
                    storeSettlement1.setAchievementZhonggong(baseProject1.getAchievementZhonggong());
                    storeSettlement1.setAchievementTechnician(baseProject1.getAchievementTechnician());
                    storeSettlement1.setAchievementSmall(baseProject1.getAchievementSmall());
                    storeSettlement1.setCommissionLarge(baseProject1.getCommissionLarge());
                    storeSettlement1.setCommissionZhonggong(baseProject1.getCommissionZhonggong());
                    storeSettlement1.setCommissionTechnician(baseProject1.getCommissionTechnician());
                    storeSettlement1.setCommissionSmall(baseProject1.getCommissionSmall());
                    storeSettlement1.setCardAchievementLarge(baseProject1.getCardAchievementLarge());
                    storeSettlement1.setCardAchievementZhonggong(baseProject1.getCardAchievementZhonggong());
                    storeSettlement1.setCardAchievementTechnician(baseProject1.getCardAchievementTechnician());
                    storeSettlement1.setCardAchievementSmall(baseProject1.getCardAchievementSmall());
                    storeSettlement1.setCardCommissionLarge(baseProject1.getCardCommissionLarge());
                    storeSettlement1.setCardCommissionZhonggong(baseProject1.getCardCommissionZhonggong());
                    storeSettlement1.setCardCommissionTechnician(baseProject1.getCardCommissionTechnician());
                    storeSettlement1.setCardCommissionSmall(baseProject1.getCardCommissionSmall());
                    storeSettlement1.setRenew(storeSettlement.getRenew());
                    storeSettlement1.setServicingTime(baseProject1.getServicingTime());
                    storeSettlement1.setNoDiscount(baseProject1.getNoDiscount());
                }else{
                    throw new SmsException(ResponseCode.MSG,"该技术已经上钟服务，不允许修改服务项目");
                }

            }

            if(!storeSettlement.getProjectId().equals(storeSettlement1.getProjectId())){
                if(null == storeSettlement1.getServiceStartDate()){

                    BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(storeSettlement.getProjectId());
                    storeSettlement1.setProjectName(baseProject1.getProjectName());
                    storeSettlement1.setProjectName(baseProject1.getProjectName());
                    storeSettlement1.setAchievementZhonggong(baseProject1.getAchievementZhonggong());
                    storeSettlement1.setAchievementTechnician(baseProject1.getAchievementTechnician());
                    storeSettlement1.setAchievementSmall(baseProject1.getAchievementSmall());
                    storeSettlement1.setCommissionLarge(baseProject1.getCommissionLarge());
                    storeSettlement1.setCommissionZhonggong(baseProject1.getCommissionZhonggong());
                    storeSettlement1.setCommissionTechnician(baseProject1.getCommissionTechnician());
                    storeSettlement1.setCommissionSmall(baseProject1.getCommissionSmall());
                    storeSettlement1.setCardAchievementLarge(baseProject1.getCardAchievementLarge());
                    storeSettlement1.setCardAchievementZhonggong(baseProject1.getCardAchievementZhonggong());
                    storeSettlement1.setCardAchievementTechnician(baseProject1.getCardAchievementTechnician());
                    storeSettlement1.setCardAchievementSmall(baseProject1.getCardAchievementSmall());
                    storeSettlement1.setCardCommissionLarge(baseProject1.getCardCommissionLarge());
                    storeSettlement1.setCardCommissionZhonggong(baseProject1.getCardCommissionZhonggong());
                    storeSettlement1.setCardCommissionTechnician(baseProject1.getCardCommissionTechnician());
                    storeSettlement1.setCardCommissionSmall(baseProject1.getCardCommissionSmall());
                    storeSettlement1.setRenew(storeSettlement.getRenew());
                    storeSettlement1.setServicingTime(baseProject1.getServicingTime());
                    storeSettlement1.setNoDiscount(baseProject1.getNoDiscount());
                }else{
                    throw new SmsException(ResponseCode.MSG,"该技术已经上钟服务，不允许修改服务项目");
                }

            }
        }


        if(null != storeSettlement.getRenew()){
//判断修改加钟
            if(storeSettlement1.getRenew()!= storeSettlement.getRenew()){
                //判断是否加钟
                if(storeSettlement.getRenew()==1){
                    storeSettlement1.setRenew(storeSettlement.getRenew());
                }else{
                    if(null != storeSettlement1.getServiceStartDate()){
                        throw new SmsException(ResponseCode.MSG,"该技师已经上钟服务，不允许取消加钟");
                    }else{
                        storeSettlement1.setRenew(storeSettlement.getRenew());
                    }
                }
            }
        }


        if(null != storeSettlement.getServiceTechnicianOneStaffId()){
            //判断是否有更换技师
            if(!storeSettlement1.getServiceTechnicianOneStaffId().equals(storeSettlement.getServiceTechnicianOneStaffId())){
                ServiceOrder serviceOrder = new ServiceOrder();
                serviceOrder.setStaffId(storeSettlement.getServiceTechnicianOneStaffId());
                serviceOrder.setStoreId(storeSettlement.getStoreId());
                ServiceOrder serviceOrder1 = ssmServiceOrderService.selectOne(serviceOrder);
                serviceOrder1.setState(0);
                int i = ssmServiceOrderService.updateNotNull(serviceOrder1);
                if(i>0){
                    storeSettlement1.setServiceTechnicianOneStaffId(storeSettlement.getServiceTechnicianOneStaffId());
                    storeSettlement1.setTechnicianOneNumber(storeSettlement.getTechnicianOneNumber());
                    storeSettlement1.setServiceTechnicianOneStaffName(storeSettlement.getServiceTechnicianOneStaffName());
                    storeSettlement1.setServiceTechnicianOneIdentityCard(storeSettlement.getServiceTechnicianOneIdentityCard());
                    storeSettlement1.setServiceTechnicianOneRatio(storeSettlement.getServiceTechnicianOneRatio());
                    ServiceOrder serviceOrders = new ServiceOrder();
                    serviceOrders.setStaffId(storeSettlement1.getServiceTechnicianOneStaffId());
                    serviceOrders.setStoreId(storeSettlement1.getStoreId());
                    ServiceOrder serviceOrder2 = ssmServiceOrderService.selectOne(serviceOrders);
                    if(serviceOrder2.getState()==0){
                        serviceOrder2.setState(1);
                        ssmServiceOrderService.updateNotNull(serviceOrder2);
                    }else{
                        throw new SmsException(ResponseCode.MSG,"该技师非空闲状态");
                    }
                }else{
                    throw new SmsException(ResponseCode.MSG,"更换技师失败");
                }
            }
        }


        return dao.updateByPrimaryKeySelective(storeSettlement1);
    }

    @Override
    public int tuifang(StoreSettlementRequest serviceOrdersRequest, String phone) throws SmsException {
        StoreSettlement storeSettlement = new StoreSettlement();
        storeSettlement.setRoomId(serviceOrdersRequest.getId());
        List<StoreSettlement> select = dao.select(storeSettlement);
        for (StoreSettlement settlement : select) {
            int i = dao.deleteByPrimaryKey(settlement.getId());
            if(i>0){
                ServiceOrder serviceOrder = new ServiceOrder();
                serviceOrder.setStoreId(settlement.getStoreId());
                List<ServiceOrder> serviceOrders = ssmServiceOrderService.select(serviceOrder);
                serviceOrder.setStaffId(settlement.getServiceTechnicianOneStaffId());
                ServiceOrder serviceOrder1 = ssmServiceOrderService.selectOne(serviceOrder);
                serviceOrder1.setState(0);
                serviceOrder1.setRow(serviceOrders.get(serviceOrders.size()-1).getRow()+1);
                serviceOrder1.setServiceStartDate(null);
                serviceOrder1.setServicingTime(null);
                int updates = ssmServiceOrderService.updates(serviceOrder1);
                if(updates==0){
                    throw new SmsException(ResponseCode.MSG,"退房失败");
                }
            }else{
                throw new SmsException(ResponseCode.MSG,"退房失败");
            }
        }
        if(select.size()>0){
            StoreRoom storeRoom = new StoreRoom();
            storeRoom.setId(select.get(0).getRoomId());
            StoreRoom storeRoom1 = ssmStoreRoomService.selectByPrimaryKey(storeRoom.getId());
            storeRoom1.setOnState(0);
            int i = ssmStoreRoomService.updateNotNull(storeRoom1);
            if(i>0){
                StoreRoomHand storeRoomHand = new StoreRoomHand();
                storeRoomHand.setRoomId(storeRoom1.getId());
                List<StoreRoomHand> select1 = ssmStoreRoomHandService.select(storeRoomHand);
                for (StoreRoomHand roomHand : select1) {
                    roomHand.setState(0);
                    int i1 = ssmStoreRoomHandService.updateNotNull(roomHand);
                    if(i1==0){
                        throw new SmsException(ResponseCode.MSG,"退房失败");
                    }
                }
            }else{
                throw new SmsException(ResponseCode.MSG,"退房失败");
            }
        }else{
            StoreRoom storeRoom = new StoreRoom();
            storeRoom.setId(serviceOrdersRequest.getId());
            StoreRoom storeRoom1 = ssmStoreRoomService.selectByPrimaryKey(storeRoom.getId());
            storeRoom1.setOnState(0);
            int i = ssmStoreRoomService.updateNotNull(storeRoom1);
            if(i>0){
                StoreRoomHand storeRoomHand = new StoreRoomHand();
                storeRoomHand.setRoomId(storeRoom1.getId());
                List<StoreRoomHand> select1 = ssmStoreRoomHandService.select(storeRoomHand);
                for (StoreRoomHand roomHand : select1) {
                    roomHand.setState(0);
                    int i1 = ssmStoreRoomHandService.updateNotNull(roomHand);
                    if(i1==0){
                        throw new SmsException(ResponseCode.MSG,"退房失败");
                    }
                }
            }else{
                throw new SmsException(ResponseCode.MSG,"退房失败");
            }
        }


        return 1;
    }


    @Override
    public int roomChange(List<StoreSettlementRequest> serviceOrdersRequest, String phone) throws SmsException {
        if(serviceOrdersRequest.size()==0){
            throw new SmsException(ResponseCode.MSG,"该房间单据为空");
        }
        int a=0;
        for (StoreSettlementRequest storeSettlementRequest : serviceOrdersRequest) {
            StoreSettlement storeSettlement = new StoreSettlement();
            BeanUtils.copyProperties(storeSettlementRequest,storeSettlement);
            int update = dao.updateByPrimaryKeySelective(storeSettlement);
            a=a+update;
        }
        if(a!=serviceOrdersRequest.size()){
            throw new SmsException(ResponseCode.MSG,"换房失败");
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int detele(StoreSettlementRequest serviceOrdersRequest, String phone) throws SmsException {
        int i = 0;
        StoreSettlement storeSettlement = dao.selectByPrimaryKey(serviceOrdersRequest.getId());
        if(null != storeSettlement.getServiceStartDate()){
            ClockService clockService = new ClockService();
            BeanUtils.copyProperties(storeSettlement,clockService);
            StaffResponse staffResponse = new StaffResponse();
            Set<String> keys = redisClient.keys(getStaffKey(phone) + "*");
            //从redis查询 设置录单人信息 app使用
            if (keys.size() > 0){
                String key =  new ArrayList<>(keys).get(0);
                staffResponse = redisClient.get(key);
            }
            clockService.setUpdateBy(staffResponse.getStaffName());
            clockService.setUpdateDate(new Date());
            int save = ssmClockServiceService.save(clockService);
            if(save>0){
                i = dao.deleteByPrimaryKey(storeSettlement.getId());
                if(i==0){
                    throw new SmsException(ResponseCode.MSG,"删除失败");
                }
            }
        }else{
            i = dao.deleteByPrimaryKey(storeSettlement.getId());
        }
        ServiceOrder serviceOrder = new ServiceOrder();
        serviceOrder.setStaffId(storeSettlement.getServiceTechnicianOneStaffId());
        ServiceOrder serviceOrder1 = ssmServiceOrderService.selectOne(serviceOrder);
        if(null != serviceOrder1){
            serviceOrder1.setState(0);
            int i1 = ssmServiceOrderService.updateNotNull(serviceOrder1);
        }

        StoreSettlement storeSettlement1 = new StoreSettlement();
        storeSettlement1.setRoomHandId(storeSettlement.getRoomHandId());
        List<StoreSettlement> select = dao.select(storeSettlement1);
        if(select.size()==0){
            StoreRoomHand storeRoomHand = new StoreRoomHand();
            storeRoomHand.setId(storeSettlement.getRoomHandId());
            StoreRoomHand storeRoomHand1 = ssmStoreRoomHandService.selectByPrimaryKey(storeRoomHand.getId());
            storeRoomHand1.setState(0);
            ssmStoreRoomHandService.updateNotNull(storeRoomHand1);

            StoreSettlement storeSettlement2 = new StoreSettlement();
            storeSettlement2.setRoomId(storeSettlement.getRoomId());
            List<StoreSettlement> select1 = dao.select(storeSettlement2);
            if(select1.size()==0){
                StoreRoom storeRoom = new StoreRoom();
                storeRoom.setId(storeSettlement.getRoomId());
                StoreRoom storeRoom1 = ssmStoreRoomService.selectByPrimaryKey(storeRoom.getId());
                storeRoom1.setOnState(0);
                ssmStoreRoomService.updateNotNull(storeRoom1);
            }
        }

        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTime(StoreSettlementRequest serviceOrdersRequest, String phone) throws SmsException {
        Date date = new Date();
        StoreSettlement storeSettlement1 = new StoreSettlement();
        storeSettlement1.setId(serviceOrdersRequest.getId());
        StoreSettlement storeSettlement = dao.selectByPrimaryKey(storeSettlement1.getId());
        storeSettlement.setServiceStartDate(date);
        int update = dao.updateByPrimaryKeySelective(storeSettlement);
        if(update>0){
            ServiceOrder serviceOrder = new ServiceOrder();
            serviceOrder.setStaffId(storeSettlement.getServiceTechnicianOneStaffId());
            serviceOrder.setStoreId(storeSettlement.getStoreId());
            ServiceOrder serviceOrder1 = ssmServiceOrderService.selectOne(serviceOrder);
            serviceOrder1.setServiceStartDate(date);
            serviceOrder1.setServicingTime(storeSettlement.getServicingTime());
            return ssmServiceOrderService.updateNotNull(serviceOrder1);
        }else{
            throw new SmsException(ResponseCode.MSG,"上钟失败，系统异常");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTimeX(StoreSettlementRequest serviceOrdersRequest, String phone) throws SmsException {
        Date date = new Date();
        StoreSettlement storeSettlement = dao.selectByPrimaryKey(serviceOrdersRequest.getId());
        storeSettlement.setServiceEndDate(date);
        int update = dao.updateByPrimaryKeySelective(storeSettlement);
        if(update>0){
            StoreSettlement storeSettlement1 = new StoreSettlement();
            storeSettlement1.setStaffId(storeSettlement.getServiceTechnicianOneStaffId());
            storeSettlement1.setStoreId(storeSettlement.getStoreId());
            List<StoreSettlement> select = dao.select(storeSettlement1);
            List<StoreSettlement> collect = select.stream().filter(storeSettlement2 -> storeSettlement2.getServiceEndDate() == null).collect(Collectors.toList());
            if(collect.size()>0){
                ServiceOrder serviceOrder = new ServiceOrder();
                serviceOrder.setStaffId(storeSettlement.getServiceTechnicianOneStaffId());
                serviceOrder.setStoreId(storeSettlement.getStoreId());
                ServiceOrder serviceOrder1 = ssmServiceOrderService.selectOne(serviceOrder);
                serviceOrder1.setServiceStartDate(null);
                serviceOrder1.setServicingTime(null);
                return ssmServiceOrderService.updates(serviceOrder1);
            }else{//重新排序
                ServiceOrder serviceOrder = new ServiceOrder();
                serviceOrder.setStoreId(storeSettlement.getStoreId());
                List<ServiceOrder> serviceOrders = ssmServiceOrderService.select(serviceOrder);
                serviceOrder.setStaffId(storeSettlement.getServiceTechnicianOneStaffId());
                ServiceOrder serviceOrder1 = ssmServiceOrderService.selectOne(serviceOrder);
                serviceOrder1.setState(0);
                serviceOrder1.setRow(serviceOrders.get(serviceOrders.size()-1).getRow()+1);
                serviceOrder1.setServiceStartDate(null);
                serviceOrder1.setServicingTime(null);
                return ssmServiceOrderService.updateByPrimaryKey(serviceOrder1);

            }
        }else{
            throw new SmsException(ResponseCode.MSG,"上钟失败，系统异常");
        }
    }

    @Override
    public PageInfo<StoreSettlement> selectPage(StoreSettlementRequest serviceOrdersRequest) throws SmsException {
        Example example = new Example(StoreSettlement.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("serviceTechnicianOneStaffId",serviceOrdersRequest.getServiceTechnicianOneStaffId());
        PageHelper.startPage(serviceOrdersRequest.getPageNum(),serviceOrdersRequest.getPageSize(),serviceOrdersRequest.getOrderAndSort());
        List<StoreSettlement> list = selectByExample(example);
        PageInfo<StoreSettlement> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    public String getStaffKey(String phone){
        return staffKey + phone;
    }

}
