package com.dsk.biz.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanException;
import com.dsk.biz.domain.Customer;
import com.dsk.biz.domain.bo.CustomerBusinessSearchDto;
import com.dsk.biz.domain.bo.CustomerSearchDto;
import com.dsk.biz.domain.vo.CustomerBusinessListVo;
import com.dsk.biz.domain.vo.CustomerListVo;
import com.dsk.biz.domain.vo.CustomerVo;
import com.dsk.biz.service.ICustomerService;
import com.dsk.biz.utils.ExcelUtils;
import com.dsk.common.annotation.Log;
import com.dsk.common.annotation.RepeatSubmit;
import com.dsk.common.core.controller.BaseController;
import com.dsk.common.core.domain.AjaxResult;
import com.dsk.common.core.domain.PageQuery;
import com.dsk.common.core.domain.R;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.enums.BusinessType;
import com.dsk.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户相关
 *
 * @author lcl
 * @create 2023/5/16
 */
@RestController
@RequestMapping("/customer")
public class CustomerController extends BaseController {

    @Autowired
    private ICustomerService baseService;

    /**
     * 查询客户列表
     */
//    @SaCheckPermission("'customer:list')")
    @GetMapping("/list")
    public TableDataInfo<CustomerListVo> selectPageList(CustomerSearchDto dto , PageQuery pageQuery) {
        return baseService.selectList(dto,pageQuery);
    }

    /**
     * 客户详情
     */
//    @SaCheckPermission("'customer:info')")
    @GetMapping("/{customerId}")
    public R<CustomerVo> info(@PathVariable("customerId") String customerId) {
        return R.ok(baseService.info(customerId));
    }

    /**
     * 添加客户
     */
//    @SaCheckPermission("'customer:add')")
    @PostMapping()
    @RepeatSubmit
    public R<Void> add(@RequestBody Customer customer) {
        return toAjax(baseService.add(customer));
    }

    /**
     * 认领客户
     */
//    @SaCheckPermission("'customer:claim'")
    @PostMapping("/claim")
    @RepeatSubmit
    public R<Map<String, Object>> claim(@RequestBody Customer customer) {
//        if (ObjectUtils.isEmpty(customer.getUipId())) throw new BeanException("城投uipId不能为空");
        boolean add = baseService.add(customer);
        if (add) {
            Map<String, Object> map = new HashMap<>();
            map.put("customerId", customer.getCustomerId());
            return R.ok(map);
        }
        return R.fail();
    }

    /**
     * 编辑客户
     */
//    @SaCheckPermission("'customer:edit'")
    @PutMapping()
    @RepeatSubmit
    public R<Void> edit(@RequestBody Customer customer) {
        return toAjax(baseService.edit(customer));
    }

    /**
     * 获取个人客户列表
     */
    @GetMapping("/user/list")
    public R<List<Customer>> userList() {
        return R.ok(baseService.selectUserList());
    }

    /**
     * 获取客户项目（合作情况）列表
     */
//    @SaCheckPermission("'customer:business:list'")
    @GetMapping("/business/list")
    public TableDataInfo<CustomerBusinessListVo> businessList(CustomerBusinessSearchDto dto, PageQuery pageQuery) {
        return baseService.selectBusinessList(dto,pageQuery);
    }

    @Log(title = "客户数据导入", businessType = BusinessType.IMPORT)
    //@SaCheckPermission("'customer:importData'")
    @PostMapping("/importData")
//    public R<List<String>> importData(@RequestPart("file") MultipartFile file) throws Exception {
    public AjaxResult importData(@RequestPart("file") MultipartFile file) throws Exception {
        List<Customer> customerList = new ExcelUtils<>(Customer.class).importExcel(file.getInputStream(), 2);
        List<String> resultList = new ArrayList<>();
        int successCount = 0;
        for (Customer customer : customerList) {
            if (ObjectUtils.isEmpty(customer.getCompanyName())) {
                continue;
            }
            try {
                baseService.add(customer);
                successCount++;
            } catch (Exception e) {
                resultList.add(customer.getCompanyName().concat(":").concat(e.getMessage()));
            }
        }
        AjaxResult success = AjaxResult.success(resultList);
        success.put("successCount", successCount);
        return success;
    }

    /**
     * 客户状态
     */
    @GetMapping("/status/{companyName}")
    public R<Integer> status(@PathVariable String companyName) {
        return R.ok(baseService.status(companyName));
    }


    /**
     * 取消认领
     */
    @PutMapping("/cancelClaim/{companyName}")
    @RepeatSubmit
    public R<Void> cancelClaim(@PathVariable String companyName) {
        return toAjax(baseService.cancelClaim(companyName));
    }

    /**
     * 历史认领
     */
    @PutMapping("/historyClaim/{companyName}")
    @RepeatSubmit
    public R<Void> historyClaim(@PathVariable String companyName) {
        return toAjax(baseService.historyClaim(companyName));
    }
}
