package com.mortals.xhx.busiz;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.utils.ServletUtils;
import com.mortals.xhx.base.system.idgenerator.service.IdgeneratorService;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.hik.face.model.req.img.ImgReq;
import com.mortals.xhx.module.hik.face.service.IHikFaceService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.service.StaffService;
import com.mortals.xhx.module.workman.model.WorkmanEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 设备
 *
 * @author: zxfei
 * @date: 2021/8/24 20:28
 */
@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {

    @Autowired
    private IdgeneratorService idgeneratorService;

    @Autowired
    private ICacheService cacheService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private UserService userService;
    @Autowired
    private IHikFaceService faceService;
    @Autowired
    private UploadService uploadService;


    //    @GetMapping("webservice")@GetMapping("idGens")
//    public String idGens() {
//        log.info("测试id生成");
//
//        String stringId = idgeneratorService.getLongId(IdgeneratorServiceImpl.IdGeneratorKey.EFFECT).toString();
//
//
//        return stringId;
//    }
    public String webservice() {
        log.info("测试");

        return "ok";
    }

    //
    @GetMapping("updatePersons")
    @UnAuth
    public String updatePersons() {
        log.info("更新用户手机号码");

        cacheService.select(1);
        Map<String, String> map = cacheService.hgetAll("com:mortals:xhx:module:workman:model:WorkmanEntity");
        cacheService.select(14);
        Map<String, WorkmanEntity> userMap = new HashMap<>();

        map.entrySet().stream().forEach(item -> {
            String value = item.getValue();
            WorkmanEntity workmanEntity = JSONObject.parseObject(value, WorkmanEntity.class);
            userMap.put(workmanEntity.getName(), workmanEntity);
        });

        List<StaffEntity> staffEntities = staffService.find(new StaffQuery());
        ArrayList<StaffEntity> updateList = new ArrayList<>();

        for (StaffEntity staffEntity : staffEntities) {
            if (ObjectUtils.isEmpty(staffEntity.getPhoneNumber())) {
                WorkmanEntity workmanEntity = userMap.get(staffEntity.getName());
                if (!ObjectUtils.isEmpty(workmanEntity)) {
                    staffEntity.setPhoneNumber(workmanEntity.getMobile());
                    updateList.add(staffEntity);
                }
            }
        }
        if (!ObjectUtils.isEmpty(updateList)) {
            log.info("更新数量：" + updateList.size());
            for (StaffEntity staffEntity : updateList) {
                staffService.update(staffEntity);
                //更新绑定的系统用户手机号码
                UserQuery userQuery = new UserQuery();
                userQuery.setCustomerId(staffEntity.getId());
                UserEntity userEntity = userService.selectOne(userQuery);
                if (userEntity != null) {
                    Map<String, Object> condition = new HashMap<>(1);
                    condition.put("id", userEntity.getId());
                    Map<String, Object> data = new HashMap<>(1);
                    data.put("mobile", staffEntity.getPhoneNumber());
                    userService.getUserDao().update(data, condition);
                }
            }
        }

        return "更新数量：" + updateList.size();
    }


    @GetMapping("downPersonsPic")
    @UnAuth
    public String downPersonsPic() {
        List<StaffEntity> staffEntities = staffService.find(new StaffQuery());
        ArrayList<StaffEntity> updateList = new ArrayList<>();
        for (StaffEntity staffEntity : staffEntities) {
            if (!ObjectUtils.isEmpty(staffEntity.getPicUri())) {
                ImgReq imgReq = new ImgReq();
                imgReq.setUrl(staffEntity.getPicUri());
                Rest<String> rest = faceService.downloadPicture(imgReq);
                if (YesNoEnum.YES.getValue() == rest.getCode()) {
                    byte[] bytes = Base64.decode(rest.getData());
                    if (!org.springframework.util.ObjectUtils.isEmpty(bytes)) {
                        InputStream inputStream = new ByteArrayInputStream(bytes);
                        MultipartFile file = ServletUtils.getMultipartFile(inputStream, "snap.jpg");
                        String filePath = uploadService.saveFileUpload(file, "file/fileupload/snap/" + DateUtil.today(), null);
                        staffEntity.setPhotoPath(filePath);

                        updateList.add(staffEntity);
                    }
                } else {
                    log.info("download:{}", JSON.toJSONString(rest));
                }
            }
        }
        if (!ObjectUtils.isEmpty(updateList)) {
            staffService.update(updateList);
        }


        return "更新数量：" + updateList.size();
    }


    public static void main(String[] args) {

    }


}
