package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.filter.LoginInterceptor;
import com.ssm.manage.dao.BlackListDao;
import com.ssm.manage.dao.StaffTemporaryDao;
import com.ssm.manage.dto.BlackListDto;
import com.ssm.manage.entity.BlackList;
import com.ssm.manage.entity.StaffTemporary;
import com.ssm.manage.response.*;
import com.ssm.manage.service.JobContentListService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/zhongkongTest")
@Slf4j
public class ZhongkongTestController {

    @Value("${zhongkong.key}")
    private String key;
    @Value("${zhongkong.host}")
    private String host;

    @Autowired
    private JobContentListService jobContentListService;
    @Autowired
    private StaffTemporaryDao staffTemporaryDao;
    @Autowired
    private BlackListDao blackListDao;


    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping
    @ApiOperation("获取中控密钥")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
//            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
//    })
//@RequiresPermissions("system:blackList:query")
    public GenericResponse listPage() {
        GenericResponse<Map<String,String>> pageResponse = new GenericResponse<>();
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        Map<String,String> map = new HashMap<>();
        map.put("key",key);
        map.put("host",host);
        pageResponse.setData(map);
        return pageResponse;
    }

    /**
     * 分页查询
     *
     * @return
     */
    @PostMapping("/getJobContext")
    @ApiOperation("获取考勤记录")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
//            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
//    })
//@RequiresPermissions("system:blackList:query")
    public PageResponse<JobContentListVo> getJobContext(HttpSession session,@RequestBody Map<String,String> parms) throws IOException, ParseException {
        PageResponse<JobContentListVo> pageResponse = new PageResponse<>();
        pageResponse.setCodeAndMsg(ResponseCode.OK);

        StaffResponse staffResponse = LoginInterceptor.getCurrentUserInfo();
        LoginInterceptor.clearThreadLocal();
        PageInfo<JobContentListVo> info = jobContentListService.getJobContext(staffResponse, parms.get("starttime"),parms.get("endtime") );
        pageResponse.setPageInfo(info);
        return pageResponse;
    }

    /**
     * 分页查询
     *
     * @return
     */
    @PostMapping("/sendZhongkong")
    @ApiOperation("发送中控")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
//            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
//    })
//@RequiresPermissions("system:blackList:query")
    public PageResponse<JobContentListVo> sendZhongkong(@RequestBody List<StaffTemporary> staffTemporaryList){
        PageResponse<JobContentListVo> pageResponse = new PageResponse<>();
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        StaffResponse staffResponse = LoginInterceptor.getCurrentUserInfo();
        LoginInterceptor.clearThreadLocal();
        for (StaffTemporary temporary : staffTemporaryList) {
//            if (temporary.getIsZhongkong()==1){
                temporary.setIsZhongkong(1);
                temporary.setSendZhongkongId(staffResponse.getId());
                temporary.setSendZhongkongName(staffResponse.getStaffName());
            if (temporary.getZhongTime()!=null) {
                temporary.setSendZhongkongTime(temporary.getZhongTime());
            }else temporary.setSendZhongkongTime(new Date());
                temporary.setZhongkongDimission(0);
                temporary.setZhongkongDimissionDate(null);
                temporary.setFacesInformation(null);
                staffTemporaryDao.updateByPrimaryKeySelective(temporary);
                BlackList blackList = new BlackList();
                Example blackExample = new Example(BlackList.class);
                blackExample.selectProperties("id","isZhongkong");
                blackExample.createCriteria().andEqualTo(BlackListDto.Fields.identityCard, temporary.getIdentityCard());
                blackList = blackListDao.selectByExample(blackExample).get(0);
//                BlackList blackList = new BlackList();
//                blackList.setIdentityCard(temporary.getIdentityCard());
//                blackList = blackListDao.select(blackList).get(0);
                blackList.setIsZhongkong(1);
                blackListDao.updateByPrimaryKeySelective(blackList);
//            }
        }
        return pageResponse;
    }


}
