package com.ssm.manage.service.impl;

import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.manage.dao.SsmClockServiceDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.ClockServiceRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class SsmClockServiceServiceImpl  extends BaseServiceImpl<ClockService> implements SsmClockServiceService {


    @Autowired
    private SsmClockServiceDao dao;

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

    @Autowired
    private SsmProjectService ssmProjectService;

    @Autowired
    private SsmStoreRoomService ssmStoreRoomService;

    @Autowired
    private SsmStoreRoomHandService ssmStoreRoomHandService;

    @Autowired
    private SsmServiceOrderService ssmServiceOrderService;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int add(ClockServiceRequest clockServiceRequest) throws SmsException {
        if(null == clockServiceRequest.getProjectId()){
            throw new SmsException(ResponseCode.MSG,"未选择服务项目");
        }
        if(null == clockServiceRequest.getServiceTechnicianOneStaffId()){
            throw new SmsException(ResponseCode.MSG,"未选择服务技师");
        }

        StoreRoomHand storeRoomHands = new StoreRoomHand();
        storeRoomHands.setId(clockServiceRequest.getRoomHandId());
        StoreRoomHand storeRoomHands1 = ssmStoreRoomHandService.selectOne(storeRoomHands);
        if(1 ==storeRoomHands1.getState()){
            throw new SmsException(ResponseCode.MSG,"该手牌当前正在被占用，请重新选择空闲手牌");
        }


        ClockService clockService = new ClockService();

        //可以删掉
//        clockService.setRoomId(clockServiceRequest.getRoomId());
//        clockService.setRoomHandId(clockServiceRequest.getRoomHandId());
//        ClockService clockService1 = dao.selectOne(clockService);
//        if(null != clockService1){
//            throw new SmsException(ResponseCode.MSG,"当前房间手牌正在被使用，请选择其他手牌或其他房间");
//        }

        BeanUtils.copyProperties(clockServiceRequest,clockService);
        BaseProject baseProject = new BaseProject();
        baseProject.setId(clockServiceRequest.getProjectId());
        BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
        clockService.setAchievementLarge(baseProject1.getAchievementLarge());
        clockService.setAchievementZhonggong(baseProject1.getAchievementZhonggong());
        clockService.setAchievementTechnician(baseProject1.getAchievementTechnician());
        clockService.setAchievementSmall(baseProject1.getAchievementSmall());
        clockService.setCommissionLarge(baseProject1.getCommissionLarge());
        clockService.setCommissionZhonggong(baseProject1.getCommissionZhonggong());
        clockService.setCommissionTechnician(baseProject1.getCommissionTechnician());
        clockService.setCommissionSmall(baseProject1.getCommissionSmall());
        clockService.setCardAchievementLarge(baseProject1.getCardAchievementLarge());
        clockService.setCardAchievementZhonggong(baseProject1.getCardAchievementZhonggong());
        clockService.setCardAchievementTechnician(baseProject1.getCardAchievementTechnician());
        clockService.setCardAchievementSmall(baseProject1.getCardAchievementSmall());
        clockService.setCardCommissionLarge(baseProject1.getCardCommissionLarge());
        clockService.setCardCommissionZhonggong(baseProject1.getCardCommissionZhonggong());
        clockService.setCardCommissionTechnician(baseProject1.getCardCommissionTechnician());
        clockService.setCardCommissionSmall(baseProject1.getCardCommissionSmall());
        clockService.setNoDiscount(baseProject1.getNoDiscount());
        clockService.setState(0);
        clockService.setDocDate(new Date());
        int insert = dao.insert(clockService);
        if(insert>0){
            //修改房间状态
            StoreRoom storeRoom = new StoreRoom();
            storeRoom.setId(clockService.getRoomId());
            StoreRoom storeRoom1 = ssmStoreRoomService.selectOne(storeRoom);
            if(storeRoom1.getOnState() == 0){
                storeRoom1.setOnState(1);
                int update = ssmStoreRoomService.updateNotNull(storeRoom1);
                if(update==0){
                    throw new SmsException(ResponseCode.MSG,"使用房间失败");
                }
            }else{
                throw new SmsException(ResponseCode.MSG,"该房间未处于空闲状态");
            }

            //修改手牌床位状态
            StoreRoomHand storeRoomHand = new StoreRoomHand();
            storeRoomHand.setId(clockService.getRoomHandId());
            StoreRoomHand storeRoomHand1 = ssmStoreRoomHandService.selectOne(storeRoomHand);
            if(storeRoomHand1.getState() != 1){
                storeRoomHand1.setState(1);
                int update = ssmStoreRoomHandService.updateNotNull(storeRoomHand1);
                if(update==0){
                    throw new SmsException(ResponseCode.MSG,"占用手牌位失败");
                }
            }else{
                throw new SmsException(ResponseCode.MSG,"该房间床位正在使用");
            }

            //修改员工状态
            ServiceOrder serviceOrder = new ServiceOrder();
            serviceOrder.setStaffId(clockServiceRequest.getServiceTechnicianOneStaffId());
            serviceOrder.setStoreId(clockServiceRequest.getStoreId());
            ServiceOrder serviceOrder1 = ssmServiceOrderService.selectOne(serviceOrder);
            if(serviceOrder1.getState()==0){//空闲
                serviceOrder1.setState(1);
                int update = ssmServiceOrderService.updateNotNull(serviceOrder1);
                if(update==0){
                    throw new SmsException(ResponseCode.MSG,"更改员工状态出错");
                }

            }else{
                throw new SmsException(ResponseCode.MSG,"当前员工正在服务中，非空闲状态");
            }
        }
        return insert;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int upClock(ClockServiceRequest clockServiceRequest) throws SmsException {
        //修改员工为上钟状态
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
        String format = simpleDateFormat.format(new Date());
        ServiceOrder serviceOrder = new ServiceOrder();
        serviceOrder.setStaffId(clockServiceRequest.getServiceTechnicianOneStaffId());
        serviceOrder.setStoreId(clockServiceRequest.getStoreId());
        ServiceOrder serviceOrder1 = ssmServiceOrderService.selectOne(serviceOrder);
        if(serviceOrder1.getState()==1){//服务就绪中
            ClockService clockService = new ClockService();
            clockService.setId(clockServiceRequest.getId());
            ClockService clockService1 = dao.selectByPrimaryKey(clockService.getId());
            clockService1.setServiceStartDate(new Date());
            int update = dao.updateByPrimaryKeySelective(clockService1);
            if(update>0){
               return 1;
            }else{
                return 0;
            }
        }else{
            throw new SmsException(ResponseCode.MSG,"当前员工未关联正在服务的项目");
        }


        //对员工重新进行排序

    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int nextClock(ClockServiceRequest clockServiceRequest) throws SmsException {
        //修改员工为上钟状态
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
        String format = simpleDateFormat.format(new Date());
        ServiceOrder serviceOrder = new ServiceOrder();
        serviceOrder.setStaffId(clockServiceRequest.getServiceTechnicianOneStaffId());
        serviceOrder.setStoreId(clockServiceRequest.getStoreId());
        ServiceOrder serviceOrder1 = ssmServiceOrderService.selectOne(serviceOrder);
        if(serviceOrder1.getState()==1){//服务就绪中
            ClockService clockService = new ClockService();
            clockService.setId(clockServiceRequest.getId());
            ClockService clockService1 = dao.selectByPrimaryKey(clockService.getId());
            clockService1.setServiceEndDate(new Date());
            int update = dao.updateByPrimaryKeySelective(clockService1);
            if(update>0){
               return 1;
            }else{
                return 0;
            }
        }else{
            throw new SmsException(ResponseCode.MSG,"当前员工未关联正在服务的项目");
        }


        //对员工重新进行排序



    }
}
