package com.jjt.biz.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jjt.biz.vo.KnittingEmpVO;
import com.jjt.common.core.controller.BaseController;
import com.jjt.common.core.domain.AjaxResult;
import com.jjt.wkEmp.domain.TwinWkEmpSync;
import com.jjt.wkEmp.service.ITwinWkEmpRotaService;
import com.jjt.wkEmp.service.ITwinWkEmpSyncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 第三方数据接口
 *
 * @author wukai
 */
@Api(tags = "第三方数据接口")
@RestController
@Slf4j
public class ApiThirdController extends BaseController {

    @Resource
    ITwinWkEmpSyncService syncService;

    @Resource
    ITwinWkEmpRotaService rotaService;

    @ApiOperation("经编车间员工排班")
    @PostMapping("/api/knitting/emp/schedule")
    @CrossOrigin(origins = "*")
    public AjaxResult empSchedule(@RequestBody KnittingEmpVO vo) {
        TwinWkEmpSync sync = new TwinWkEmpSync();
        Date s = new Date();
        sync.setRecordTime(s);
        sync.setStatus("N");
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String jsonString = objectMapper.writeValueAsString(vo);
            sync.setContent(jsonString);
            syncService.insertTwinWkEmpSync(sync);
            rotaService.sync(vo);
            Date e = new Date();
            long time = e.getTime() - s.getTime();
            sync.setTime(time);
            sync.setStatus("Y");
            syncService.updateTwinWkEmpSync(sync);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
