package com.fdkankan.platform.goods.controller.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.constant.ConstantFilePath;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.platform.goods.entity.Company;
import com.fdkankan.platform.goods.request.RequestCompany;
import com.fdkankan.platform.goods.service.ICameraDetailService;
import com.fdkankan.platform.goods.service.ICompanyService;
import com.fdkankan.platform.goods.vo.CompanyVO;
import com.fdkankan.platform.user.service.IManagerService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 * 企业信息模块
 */
@Log4j2
@RestController
@RequestMapping("/service/platform/manager/company")
public class ManagerCompanyController {

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private ICameraDetailService cameraDetailService;

    @Autowired
    private IManagerService managerService;

    /**
     * 新增或修改企业信息
     * id               id有则修改，无则新增
     * companyName      企业名称
     * topLogo          顶部logo
     * markerLogo       地面点位logo
     * floorLogo        地面logo
     * qrLogo           二维码logo
     * managerId        客户账号id
     * cameraDelete     默认注册，0未注册，1注册
     * showLogo         是否显示初始logo，0隐藏，1显示
     */
    @PostMapping(value = "/insertOrUpdate")
    public String insertOrUpdate(@RequestBody RequestCompany param){
        return companyService.saveOrUpdateByParam(param);
    }

    /**
     * 根据id查询企业信息
     * id       企业id
     * @return
     */
    @PostMapping(value = "/findById")
    public Company findById(@RequestBody RequestCompany param){
        if(StringUtils.isEmpty(param.getId())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        Company company = companyService.getById(param.getId());
        if(company!=null && TbStatus.VALID.code().equals(company.getTbStatus())){
            return company;
        }
        return null;
    }

    /**
     * 删除企业信息
     * id       企业id
     * @return
     */
    @PostMapping(value = "/deleteById")
    public String deleteById(@RequestBody RequestCompany param){
        if(StringUtils.isEmpty(param.getId())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        companyService.removeById(param.getId());
        cameraDetailService.updateCompanyId(param.getId());
        return ServerCode.SUCCESS.message();
    }

    /**
     * 企业列表
     * companyName      企业名称
     */
    @PostMapping(value = "/list")
    public Page<CompanyVO> list(@RequestBody RequestCompany param){
        return companyService.pageList(param);
    }

    /**
     * 所有企业
     * @return
     */
    @PostMapping(value = "/listAll")
    public List<Company> listAll(){
        return companyService.list(new QueryWrapper<Company>().lambda().eq(Company::getTbStatus,TbStatus.VALID.code()));
    }

    /**
     * 上传企业logo
     * file         文件流
     * @return
     */
    @PostMapping("uploadLogoImg")
    public String uploadLogoImg(@RequestParam(value = "file",required = false) MultipartFile file) throws Exception {
        String filePath = ConstantFilePath.AGENT_PATH + "company/";
        Long date = System.currentTimeMillis();
        String fileName = "logo" + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        File targetFile = new File(filePath + File.separator + date + "_" + fileName);
        if(!targetFile.getParentFile().exists()){
            targetFile.getParentFile().mkdirs();
        }
        file.transferTo(targetFile);
        return  "/agent/company/" + date + "_" + fileName;
    }
}
